var是可选的吗?
myObj = 1;
和?
var myObj = 1;
我发现他们都工作从我的测试,我假设var是可选的。对吗?
var是可选的吗?
myObj = 1;
和?
var myObj = 1;
我发现他们都工作从我的测试,我假设var是可选的。对吗?
它们不一样。
未声明的变量(没有var)被视为全局对象的属性。(通常是窗口对象,除非你在with块中)
用var声明的变量是正常的局部变量,在声明它们的函数之外是不可见的。(注意Javascript没有块作用域)
更新:ECMAScript 2015
let是在ECMAScript 2015中引入的,具有块作用域。
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,并偶然地将一个通用变量名绑定到全局对象。这会产生奇怪且难以调试的行为。
Var是可选的。Var将变量置于局部作用域。如果一个变量在定义时没有使用var,那么它是在全局作用域中,是不可删除的。
edit
我认为不可删除的部分在特定的环境下在某个时间点是正确的。我一定是在做梦。
不,它们不相等。
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”的值,但第一个函数不会。
Javascript中的var关键字是有目的的。
如果你声明一个没有var关键字的变量,就像这样:
myVar = 100;
它变成了一个全局变量,可以从脚本的任何部分访问。如果你不是故意这样做的,或者没有意识到这一点,如果你在javascript的另一个地方重复使用变量名,它会给你带来痛苦。
如果你用var关键字来声明变量,就像这样:
var myVar = 100;
它是作用域的局部({]-括号,函数,文件,取决于你把它放在哪里)。
这是一种更安全的处理变量的方法。因此,除非你是故意这样做,尽量声明变量与var关键字,而不是没有。
想想今天在StackOverflow上被问到的这个问题:
简单的Javascript问题
上面的场景是一个很好的测试和一个实际的例子…… 开发人员在他的一个变量中使用了JavaScript函数的名称。
代码有什么问题? 该代码仅在用户第一次单击按钮时起作用。
解决方案是什么? 在变量名之前添加var关键字。
这不仅仅是局部和全局的区别。使用var创建的全局变量与不使用var创建的全局变量不同。考虑一下:
var foo = 1; // declared properly
bar = 2; // implied global
window.baz = 3; // global via window object
根据到目前为止的答案,这些全局变量foo、bar和baz都是等价的。事实并非如此。使用var创建的全局变量被(正确地)分配了内部[[DontDelete]]属性,这样它们就不能被删除。
delete foo; // false
delete bar; // true
delete baz; // true
foo; // 1
bar; // ReferenceError
baz; // ReferenceError
这就是为什么你应该总是使用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中最重要的事情。
围绕着这个问题有太多的困惑,现有的答案没有一个能清晰直接地涵盖一切。下面是一些内联注释的例子。
//this is a declaration
var foo;
//this is an assignment
bar = 3;
//this is a declaration and an assignment
var dual = 5;
声明设置了一个DontDelete标志。作业则不然。
声明将该变量绑定到当前作用域。
赋值但未声明的变量将寻找要附加到的作用域。这意味着它将遍历范围的食物链,直到找到具有相同名称的变量。如果没有找到,它将被附加到顶层作用域(通常称为全局作用域)。
function example(){
//is a member of the scope defined by the function example
var foo;
//this function is also part of the scope of the function example
var bar = function(){
foo = 12; // traverses scope and assigns example.foo to 12
}
}
function something_different(){
foo = 15; // traverses scope and assigns global.foo to 15
}
为了非常清楚地描述发生了什么,本文对delete函数的分析广泛地涵盖了变量实例化和赋值。
Var不允许程序员声明变量,因为Javascript没有变量。Javascript有对象。Var显式地向未定义的对象声明一个名称。赋值将一个名称作为句柄分配给已赋值的对象。
使用var告诉Javacript解释器两件事:
不使用委托反向遍历查找值的名称,而是使用此名称 不删除名字
省略var告诉Javacript解释器使用第一个找到的具有相同名称的对象的前一个实例。
Var作为关键字源于语言设计者的错误决定,就像Javascript作为名称源于错误决定一样。
ps.学习上面的代码示例。
看看这个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
I just found the answer from a forum referred by one of my colleague. If you declare a variable outside a function, it's always global. No matter if you use var keyword or not. But, if you declare the variable inside a function, it has a big difference. Inside a function, if you declare the variable using var keyword, it will be local, but if you declare the variable without var keyword, it will be global. It can overwrite your previously declared variables. - See more at: http://forum.webdeveloperszone.com/question/what-is-the-difference-between-using-var-keyword-or-not-using-var-during-variable-declaration/#sthash.xNnLrwc3.dpuf
撇开范围不谈,它们可以用不同的方式使用。
console.out(var myObj=1);
//SyntaxError: Unexpected token var
console.out(myObj=1);
//1
语句和表达式
不,这不是“必需的”,但如果你不这样做,可能会导致重大问题。不使用var定义变量,将该变量置于其所在代码部分的作用域内。如果不这样做,那么它就不包含在该作用域中,并且可以覆盖先前定义的在函数作用域之外的同名变量。