Scope:
1.Global scope(global) 2.Function scope(function)
Global scope
var a=1; console.log(a);//1 //{}表示语句块 if(a==1){ var b=2; console.log(b);//2 } console.log(b); for (var c = 0; c < 10; c++) { ; }; console.log(c); function fn(){ var d=3; console.log(d); } fn(); //console.log(d);//报错 console.log("---window---"); console.log(a);//1 console.log(b);//2 console.log(c);//10 console.log(d);//报错 //全局变量 挂载在window对象的属性。 //声明变量有前置功能(hosting hot) //函数也有前置功能 console.log(c); var c;//undefined
Function scope
var a=1; function fn(){ console.log(a);//1 /*console.log(a);//undefined var a=2;*/ } fn();
The above is the detailed content of Discussion about scope in JavaScript ES6. For more information, please follow other related articles on the PHP Chinese website!