javascript – 函数提升更改var值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 函数提升更改var值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5419字,纯文字阅读大概需要8分钟。
内容图文
![javascript – 函数提升更改var值](/upload/InfoBanner/zyjiaocheng/728/d2feffcbd2fc48e095379b034f3db883.jpg)
任何人都可以解释我为什么会得到不同的输出?
代码1:
var a = 1;
function b() {
a = 10;
console.log(a); //output 10
}
b();
console.log(a); //output 10
代码2:
var a = 1;
function b() {
a = 10;
console.log(a); //output 10
function a() {}
}
b();
console.log(a); //output 1
为什么我在调用函数“b”后得到“a”变量的不同输出?需要一些明确的解释这里真正发生了什么?
解决方法:
我相信我们的OP尝试了互联网学习的权衡.信息的可访问性,但没有指南.不幸的是,我们不知道你知道多少,我们承担了很多事情,而那些你知道的事情很少,这些知识最终导致了解可怕的吊装是什么.这是典型的how magnets work,我将提供一些关键字,作为您理解JavaScript的指导.
创建阶段激活又名代码执行阶段=执行上下文
好的但是那些是什么?
范围链创建’this’关键字的参数,函数,变量值=创建阶段和
分配值和函数引用,然后执行代码=激活又代码执行阶段
>创作阶段
>设置范围链
>创建将包含变量的对象
>创建参数对象
>搜索函数声明,并为每个函数声明,在上面的对象上创建具有该名称的属性,如果函数名存在,那么ref指针值将被覆盖
>搜索变量声明,并为每个声明声明在上面的对象中创建具有该名称的属性,同时将其值设置为undefined.如果名称已存在,则不执行任何操作.
>已解决此关键字的值.
>执行阶段又称运行代码阶段
>分配函数exe()并为this = value赋值.
代码1按照上述算法逐行开始执行.
第1阶段:创作阶段
globalExeContext = {
//no scope
objForVariables = {
//no arguments because its no function
// function declarations
b: `points to function`,
// variable declarations
a: `undefined`
},
this: //not important for this example
}
创造阶段就完成了.我们按照上面定义的说明逐行进行.现在我们处于执行阶段,分配=和execution()发生.我们再一次从第1行开始
第二阶段:执行阶段
globalExeContext = {
//no scope
objForVariables = {
//no arguments because its no function
// function declarations
b: `points to function`,
// variable declarations
a: 1 // because we did the `=` on line 1
},
this: //not important for this example
}
然后在第8行,我们发现函数执行意味着在全局执行上下文之上创建新的执行上下文,这意味着我们再次对该函数使用上面的算法.
第1阶段:创作阶段
bExeContext = {
// This is the scope object, and in this object now is placed the global exe context we have worked on before
scope: {globalExeContext}
objForVariables = {
//the is arguments object for this one but its empty, because we have no arguments for this function
args:{},
// function declarations are none here
// variable declarations are none here
},
this: //not important for this example
}
现在,我们已进入执行阶段,我们执行()和=.在第1行,它被告知a = 10这意味着我们需要找到一个为其分配值.但是我们在bExeContext对象中没有?现在怎么办?现在我们进入范围并尝试在那里找到它.果然,在我们的全球空间中有一个,现在我们分配10个.我们已经覆盖了它.它将永远不会是相同的,我将带回globalExeContext来向您展示.
第二阶段:执行阶段
globalExeContext = {
//no scope
objForVariables = {
//no arguments because its no function
// function declarations
b: `points to function`,
// variable declarations
a: 10 // look what you have done
},
this: //not important for this example
}
既然已经解决了,我们回去执行b函数中的下一行,那就是console.log(a);.我们需要解决一个是什么然后又一次又一次,在b中搜索没有给我们任何东西,但我们在globalExeStack中有它.你记得,它是10.所以我们记录10.我们已经到达函数的末尾,它从堆栈中弹出.它不复存在了.
现在我们继续执行全局代码的最后一行:console.log(a);而且很简单,globalExeStack中有一个.这是10.
代码2第1阶段:创建阶段.
globalExeContext = {
//no scope
objForVariables = {
//no arguments because its no function
// function declarations
b: `points to function`,
// variable declarations
a: `undefined`
},
this: //not important for this example
}
第2阶段:执行阶段=和()
第一行是相同的
globalExeContext = {
//no scope
objForVariables = {
//no arguments because its no function
// function declarations
b: `points to function`,
// variable declarations
a: 1
},
this: //not important for this example
}
第8行告诉执行b函数.
第1阶段:创作阶段
bExeContext = {
scope: {globalExeContext}
objForVariables = {
//the is arguments object for this one but its empty, because we have no arguments for this function
args:{},
// function declarations
a: `points to function`
// variable declarations are none here
},
this: //not important for this example
}
第二阶段:执行阶段
第1行告诉找到a并为其分配10.我们这次很幸运,因为可以在我们的bExeContext中找到.
bExeContext = {
scope: {globalExeContext}
objForVariables = {
//the is arguments object for this one but its empty, because we have no arguments for this function
args:{},
// function declarations
a: 10 // no longer points to `points to function`
// variable declarations are none here
},
this: //not important for this example
}
这意味着我们没有必要去全球空间寻找.执行b函数的第2行告诉console.log(a);.我们需要解决什么是肯定的
bExeContext = {
// blablbalblablbalblablblal
// blablbalblablbalblablblalb
a: 10 // no longer points to `points to function`
// blablbalblablbalblablblalb
},
// blablbalblablbalblablblalb
}
我们确实在bExeContext上找到了它.我们记录10.我们完成了执行b功能,然后将它从执行堆栈中弹出.它不复存在了.
我们现在继续评估全球代码.我们完成了b();这是在第9行,现在我们在第10行.console.log(a);并解决我们必须先找到它.要刷新globalExeContext的外观,这是最后一张图:
globalExeContext = {
//no scope
objForVariables = {
//no arguments because its no function
// function declarations
b: `points to function`,
// variable declarations
a: 1
},
this: //not important for this example
}
果然,a是1.我们记录1.我们完成了.
你看,一旦你运行JS代码就会实现提升.这是放下代码的过程,如果你愿意,可以将其排序.更准确地说,它是1.arguments 2.function声明3.variable声明.当您的代码只有几行或一行,或根本没有任何功能时,就会发生吊装,因为一旦您尝试播放解释器的一部分并布置代码,您就会完成吊装.
内容总结
以上是互联网集市为您收集整理的javascript – 函数提升更改var值全部内容,希望文章能够帮你解决javascript – 函数提升更改var值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。