Scope JS-

 

 

Variable lift and enhance the function declaration

Variable lift

var x;
x=10;
alert(x);	//输出10
//变量提升
x=10;
var x;
alert(x);	//输出10

Two or more codes can be output correctly. Second segment var x; in the second line, but in the first row on the use of x, in fact, the compiler at compile time will variable declaration and the function declaration in advance, called the variable lift or function declaration lift

Enhance the function declaration

//函数声明提升
functionName();	//可以在函数声明前就调用
function functionName(){
	//your code
}

Plus the difference between the var and without var

function t1(){
	//在赋值时,先检测当前作用域有没有a,如果有则赋值;否则在作用域链顶级(例如window)声明变量并赋值
	var a=1; //在当前作用域声明的变量(局部变量)
	b=1; //在window下声明的变量(全局变量)
}

Guess you like

Origin www.cnblogs.com/yangjiale/p/11261382.html