var是可选的吗?
myObj = 1;
和?
var myObj = 1;
我发现他们都工作从我的测试,我假设var是可选的。对吗?
var是可选的吗?
myObj = 1;
和?
var myObj = 1;
我发现他们都工作从我的测试,我假设var是可选的。对吗?
当前回答
They mean different things. If you use var the variable is declared within the scope you are in (e.g. of the function). If you don't use var, the variable bubbles up through the layers of scope until it encounters a variable by the given name or the global object (window, if you are doing it in the browser), where it then attaches. It is then very similar to a global variable. However, it can still be deleted with delete (most likely by someone else's code who also failed to use var). If you use var in the global scope, the variable is truly global and cannot be deleted.
在我看来,这是javascript中最危险的问题之一,应该弃用,或者至少在警告中提出警告。原因是,很容易忘记var,并偶然地将一个通用变量名绑定到全局对象。这会产生奇怪且难以调试的行为。
其他回答
这是Javascript最棘手的部分之一,但也是它的核心特性之一。用var声明的变量在你声明它的地方“开始它的生命”。如果你省略了var,这就像你在谈论一个你之前使用过的变量。
var foo = 'first time use';
foo = 'second time use';
至于作用域,变量不可能自动变成全局变量。相反,Javascript将遍历作用域链,以查看之前是否使用过该变量。如果它发现了一个以前使用过的同名变量的实例,它将使用该变量以及声明它的作用域。如果它在任何地方都没有遇到变量,它将最终击中全局对象(浏览器中的窗口),并将变量附加到它。
var foo = "I'm global";
var bar = "So am I";
function () {
var foo = "I'm local, the previous 'foo' didn't notice a thing";
var baz = "I'm local, too";
function () {
var foo = "I'm even more local, all three 'foos' have different values";
baz = "I just changed 'baz' one scope higher, but it's still not global";
bar = "I just changed the global 'bar' variable";
xyz = "I just created a new global variable";
}
}
当与嵌套函数和回调函数一起使用时,此行为非常强大。学习什么是函数以及作用域如何工作是Javascript中最重要的事情。
They mean different things. If you use var the variable is declared within the scope you are in (e.g. of the function). If you don't use var, the variable bubbles up through the layers of scope until it encounters a variable by the given name or the global object (window, if you are doing it in the browser), where it then attaches. It is then very similar to a global variable. However, it can still be deleted with delete (most likely by someone else's code who also failed to use var). If you use var in the global scope, the variable is truly global and cannot be deleted.
在我看来,这是javascript中最危险的问题之一,应该弃用,或者至少在警告中提出警告。原因是,很容易忘记var,并偶然地将一个通用变量名绑定到全局对象。这会产生奇怪且难以调试的行为。
看看这个Fiddle: http://jsfiddle.net/GWr6Z/2/
function doMe(){
a = "123"; // will be global
var b = "321"; // local to doMe
alert("a:"+a+" -- b:"+b);
b = "something else"; // still local (not global)
alert("a:"+a+" -- b:"+b);
};
doMe()
alert("a:"+a+" -- b:"+b); // `b` will not be defined, check console.log
不,它们不相等。
myObj = 1;您正在使用一个全局变量。
后一种声明在您使用的范围内创建一个局部变量。
尝试下面的代码来理解差异:
external = 5;
function firsttry() {
var external = 6;
alert("first Try: " + external);
}
function secondtry() {
external = 7;
alert("second Try: " + external);
}
alert(external); // Prints 5
firsttry(); // Prints 6
alert(external); // Prints 5
secondtry(); // Prints 7
alert(external); // Prints 7
第二个函数改变全局变量“external”的值,但第一个函数不会。
撇开范围不谈,它们可以用不同的方式使用。
console.out(var myObj=1);
//SyntaxError: Unexpected token var
console.out(myObj=1);
//1
语句和表达式