这两者之间有什么区别 a = 'ABC';var b = 'ABC';



这两行代码的区别?

a = 'abc'; 
var b = 'abc';

它们只是不同的变量吗?是这样吗?

我想说是的,但我只是在学习。

第一个隐式创建一个全局变量,第二个在当前范围内创建一个变量。

这取决于。

在全球范围内,没有区别。但是,如果您在本地范围内,则有所不同。

//Both global
var test1=1;
test2=2;
function first()
{
var test1 =-1; // Local: set a new variable independent of the global test1
test2 =3;     // Change the test2 global variable to 2
console.log(test1); //will display -1 (local variable value)
}
function second()
{
console.log(test1); //will display 1 (global variable value)
}

function first()内部,test1 的值是 -1,因为我们 test1 命中了使用var创建的局部变量,function second()没有 test1 作为局部变量,因此它将显示 1。

最新更新