The concepts of scope, scope chain, execution environment, execution environment stack and this are very important in JavaScript. I often confuse them, so here is a summary;
<script type="text/javascript"> function test1(){ a = 1;//全局变量,只有在当前函数运行时,才有效 } test1(); console.log(a);//1 注意test1函数必须运行,不然找不到a </script>
<script type="text/javascript"> var b = 1;//全局变量 console.log(b === window.b); //true 全局变量可以当做window对象的属性用,他们是一样的; </script>
<script type="text/javascript"> var c = 1;//全局变量// console.log(d);//ReferenceError: d is not defined 引用错误,当前作用域就是最外层作用域,依然找不到d function test2(d){ console.log(c); //1 全局变量,哪都可以访问;(先找当前作用域,找不到,就向外层作用域找,直到window最外层,找到了) console.log(d);//3 形参是局部变量,只有当前作用域下可以访问 } test2(3); </script>
Its route has been It is fixed and has nothing to do with where the function runs;
<script type="text/javascript"> var a = 1; var b = 2; var c = 3; var d = 4; function inner(d) {//它的作用域链是inner---全局 var c = 8; console.log(a);//1 当前作用域找不到a,去全局作用域找到了a=1 console.log(b);//2 当前作用域找不到b,去全局作用域找到了b=2 console.log(c);//8 当前作用域找到了c=8 console.log(d);//7 当前作用域找到了d=7,形参也是局部作用域 // console.log(e);//ReferenceError: e is not defined 引用错误,找不到e, 它的作用域链是inner---全局 console.log(a+b+c+d);//18 } function outter(e) { var a = 5;//inner()的作用域链是inner---全局,所以这个a相当于无效 var b = 6;//inner()的作用域链是inner---全局,所以这个a相当于无效 inner(7); } outter(999);//这个999无效,里面的e根本找不到 </script>
<script type="text/javascript"> var a = 'Lily'; var b = 'Lucy'; function outer() { var b = 'Jesica'; var c = 'Susan'; function inner(c) { console.log(a);//Lily console.log(window.b);//Lucy console.log(b);//Jesica console.log(c);//Jenifer } inner('Jenifer'); } outer(); </script>
It should be noted that if the current execution environment (which stores data and variables in the current scope chain) cannot find the variable, it is When it’s not there, it won’t search the previous execution environment, which is different from the scope chain;
The execution order of the code is not all line by line, but the calling order of the function Related:Execute line 20, call the outer(999) function, then enter the outer(999) function execution environment, declare promotion, and pass the actual parameter 999 to the formal parameter e; now there are Two execution environments, outer(999) is the current execution environment;
Execute line 16, assign a=5; and then assign b=6 on line 17;
Execute line 18, call the inner(7) function, then enter the inner(7) function execution environment, declare promotion, and pass the actual parameter 7 to the formal parameter d;
Execute line 7, assign c=8; then calculate and output;
Since it is necessary to find variables on the scope chain To consume performance, we should find variables as soon as possible, so when functions are nested in multiple layers, we should use local variables inside the function as much as possible;
We can say that using global variables inside the function is a A cross-scope operation. If a cross-scope value is used multiple times inside the function, then we store it in a local variable, which can improve performance.
The above is the detailed content of JavaScript scope chain and execution environment. For more information, please follow other related articles on the PHP Chinese website!