var是可选的吗?

myObj = 1;

和?

var myObj = 1;

我发现他们都工作从我的测试,我假设var是可选的。对吗?


当前回答

围绕着这个问题有太多的困惑,现有的答案没有一个能清晰直接地涵盖一切。下面是一些内联注释的例子。

//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函数的分析广泛地涵盖了变量实例化和赋值。

其他回答

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

这是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中最重要的事情。

它们不一样。

未声明的变量(没有var)被视为全局对象的属性。(通常是窗口对象,除非你在with块中)

用var声明的变量是正常的局部变量,在声明它们的函数之外是不可见的。(注意Javascript没有块作用域)

更新:ECMAScript 2015

let是在ECMAScript 2015中引入的,具有块作用域。

不,这不是“必需的”,但如果你不这样做,可能会导致重大问题。不使用var定义变量,将该变量置于其所在代码部分的作用域内。如果不这样做,那么它就不包含在该作用域中,并且可以覆盖先前定义的在函数作用域之外的同名变量。

围绕着这个问题有太多的困惑,现有的答案没有一个能清晰直接地涵盖一切。下面是一些内联注释的例子。

//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函数的分析广泛地涵盖了变量实例化和赋值。