简体   繁体   English

使用var与函数声明javascript对象有什么区别?

[英]What is the difference between declaring javascript objects with var vs. with function?

I'm a confused newbie. 我是一个困惑的新手。 I read in a tutorial that you create a javascript object like so: 我在教程中读到你创建一个像这样的javascript对象:

function myObject() {
    this.myProperty = "a string";
    this.myMethod = function () {
        //Method code
    }
}

Then I read somewhere else that you create an object like so: 然后我在其他地方读到你创建一个像这样的对象:

var myObject = {
    myProperty: "a string",
    myMethod : function () {
        //Method code
    }
}

What is the (non-subjective) difference between the two? 两者之间的(非主观)差异是什么? Is there an official right way and a wrong way? 是否有正式的正确方式和错误的方式?

Both declarations are correct but they have different semantics. 两个声明都是正确的,但它们具有不同的语义。

The first type of declaration allows you to create instances of your objects: 第一种声明允许您创建对象的实例:

var t = new myObject();
// then use t
t.myProperty = "some value";

var otherT = new myObject();
otherT.myProperty = "some other value";

The second is almost like a static object: 第二个几乎就像一个静态对象:

myObject.myProperty = "some value";

Here is a direct comparison... 这是一个直接比较......

function myObject() {

This declares the function when JavaScript is parsed... 这解析了解析JavaScript时的函数...

var myObject = function () {

This declares the function at run time. 这在运行时声明了该函数。

If you use the "var" method, your function must be declared before you use it... try this example. 如果使用“var”方法,则必须在使用之前声明函数...尝试此示例。

myFunction(); // Works
myVarFunction(); // Boom

var myVarFunction = function () { alert("Hi"); };

function myFunction() { alert("Hi"); };

So why use the "var" method if you have to be more careful to use it? 那么为什么要使用“var”方法,如果你必须更加小心地使用它? It is all to do with the scope... scoped functions are considered better. 这与范围有关...范围函数被认为更好。

UPDATE: And there are some great explanations here: 更新:这里有一些很好的解释:

var functionName = function() {} vs function functionName() {} var functionName = function(){} vs function functionName(){}

The major difference between the two is that one variable is local and the other is global. 两者之间的主要区别在于一个变量是本地变量而另一个变量是全局变量。 “var” basically defines the scope of the variable. “var”基本上定义了变量的范围。

When we add var to a variable value assignment, javascript ensures that the variable is confined to whichever function it is assigned to and does not collide with the same name variable within another function. 当我们将var添加到变量赋值时,javascript确保变量仅限于分配给它的任何函数,并且不会与另一个函数中的同名变量冲突。

When we don't use var, then it is declared as a global function and chances of collision can happen. 当我们不使用var时,它被声明为全局函数,并且可能发生碰撞。 So it's always advisable to use “var” before variable value assignment. 因此,始终建议在变量赋值之前使用“var”。 If needed use an anonymous function for closure. 如果需要,使用匿名函数进行关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript类中声明变量:this vs. var。 区别? - Declaring variables in JavaScript class: this vs. var. Difference? 函数声明与函数声明有什么区别? JavaScript 中的定义? - What is the difference between function declaration Vs. definition in JavaScript? JavaScript 中的 (var1 !== var2) 与 (var1 && var1 !== var2) 有什么区别? - What is the difference between (var1 !== var2) vs (var1 && var1 !== var2) in JavaScript? 类方法与类字段函数与类字段箭头函数有什么区别? - What is the difference between class method vs. class field function vs. class field arrow function? 在javascript中,window.function(){}和var variable = function有什么区别? - In javascript, what is the difference between window.function(){} and var variable = function? 在JavaScript中,var a = 5有什么区别。 而var a = 5? - In JavaScript, what is the difference between var a=5. and var a=5? var a = {}和var a = function(){}有什么区别 - what is the difference between var a = {} and var a = function(){} Javascript:var myFunc = function()与var myFunc = function myFunc() - Javascript: var myFunc = function() vs. var myFunc = function myFunc() JavaScript中的var thing和function thing()有什么区别? - What is the difference between var thing and function thing() in JavaScript? Javascript - 命名空间与闭包之间的区别? - Javascript - difference between namespace vs. closure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM