注意:这个问题是从ECMAScript版本3或5的角度提出的。随着ECMAScript 6版本中新特性的引入,答案可能会过时。
JavaScript中var关键字的功能是什么
var someNumber = 2;
var someFunction = function() { doSomething; }
var someObject = { }
var someObject.someProperty = 5;
and
someNumber = 2;
someFunction = function() { doSomething; }
someObject = { }
someObject.someProperty = 5;
?
你什么时候会使用其中一个,为什么/做什么?
这是不同的。
varx=1在当前范围(也称为执行上下文)中声明变量x。如果声明出现在函数中,则声明局部变量;如果它在全局范围内,则声明一个全局变量。
另一方面,x=1仅仅是一项财产转让。它首先尝试根据作用域链解析x。如果它在该范围链中的任何位置找到它,它将执行赋值;如果找不到x,只有在全局对象(这是作用域链中的顶级对象)上创建x属性。
现在,请注意,它没有声明全局变量,而是创建了一个全局属性。
这两者之间的区别很微妙,可能会令人困惑,除非您了解变量声明也会创建财产(仅在variable Object上),并且Javascript(好吧,ECMAScript)中的每个属性都有描述其财产的特定标志——ReadOnly、DontEnum和DontDelete。
由于变量声明使用DontDelete标志创建属性,var x=1和x=1(在全局范围内执行时)之间的区别在于前者-变量声明-创建DontDelete'able属性,而后者不创建。因此,可以从全局对象中删除通过此隐式赋值创建的属性,而不能删除前一个属性(通过变量声明创建的属性)。
但这当然只是理论,在实践中,由于实现中的各种错误(例如IE中的错误),两者之间的差异甚至更大。
希望一切都有意义:)
[更新2010/12/16]
在ES5(ECMAScript 5;最近标准化,该语言的第5版)中,有一种所谓的“严格模式”——一种选择性语言模式,它稍微改变了未声明赋值的行为。在严格模式下,分配给未声明的标识符是ReferenceError。这样做的理由是捕捉意外分配,防止创建不需要的全局财产。一些较新的浏览器已经开始滚动支持严格模式。例如,请参见我的同胞表。
我看到人们在声明带有或不带有var以及函数内部或外部的变量时感到困惑。下面是一个深入的示例,将指导您完成以下步骤:
请在jsfiddle查看下面的脚本
a = 1;// Defined outside the function without var
var b = 1;// Defined outside the function with var
alert("Starting outside of all functions... \n \n a, b defined but c, d not defined yet: \n a:" + a + "\n b:" + b + "\n \n (If I try to show the value of the undefined c or d, console.log would throw 'Uncaught ReferenceError: c is not defined' error and script would stop running!)");
function testVar1(){
c = 1;// Defined inside the function without var
var d = 1;// Defined inside the function with var
alert("Now inside the 1. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);
a = a + 5;
b = b + 5;
c = c + 5;
d = d + 5;
alert("After added values inside the 1. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);
};
testVar1();
alert("Run the 1. function again...");
testVar1();
function testVar2(){
var d = 1;// Defined inside the function with var
alert("Now inside the 2. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);
a = a + 5;
b = b + 5;
c = c + 5;
d = d + 5;
alert("After added values inside the 2. function: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n d:" + d);
};
testVar2();
alert("Now outside of all functions... \n \n Final Values: \n a:" + a + "\n b:" + b + "\n c:" + c + "\n You will not be able to see d here because then the value is requested, console.log would throw error 'Uncaught ReferenceError: d is not defined' and script would stop. \n ");
alert("**************\n Conclusion \n ************** \n \n 1. No matter declared with or without var (like a, b) if they get their value outside the function, they will preserve their value and also any other values that are added inside various functions through the script are preserved.\n 2. If the variable is declared without var inside a function (like c), it will act like the previous rule, it will preserve its value across all functions from now on. Either it got its first value in function testVar1() it still preserves the value and get additional value in function testVar2() \n 3. If the variable is declared with var inside a function only (like d in testVar1 or testVar2) it will will be undefined whenever the function ends. So it will be temporary variable in a function.");
alert("Now check console.log for the error when value d is requested next:");
alert(d);
结论无论是否声明有var(如a、b),如果它们在函数外部获取值,它们都将保留其值,并且通过脚本添加到各个函数内部的任何其他值也将保留。如果在函数(如c)中声明变量时没有var,它将像前面的规则一样,从现在起在所有函数中保留其值。要么在函数testVar1()中获得第一个值,要么仍保留该值,并在函数testVar2()中获取附加值如果变量仅在函数内部用var声明(如testVar1或testVar2中的d),则每当函数结束时,它都将是未定义的。所以它将是函数中的临时变量。