简体   繁体   English

使用带有和不带有“ new”的构造函数创建对象

[英]Creating an object using a constructor with and without `new`

I have noticed I can create the same object using a constructor in two different ways. 我注意到我可以使用构造函数以两种不同的方式创建相同的对象。

var myObj = Object()

var myObj = new Object()

I can add properties to both using these methods. 我可以使用这些方法向两者添加属性。 myObj.age = 1 and myObj['age'] = 1 . myObj.age = 1myObj['age'] = 1 The properties of both can be accesed the same way. 两者的属性可以相同的方式访问。

So what is the actual difference between these two ways I created myObj ? 那么我创建myObj这两种方式之间的实际区别是什么? Also is one of these the better way to create an object? 这些方法也是创建对象的更好方法之一吗?

The difference is that the first one simply calls Object() as a function, within the scope of the window object. 不同之处在于,第一个函数只是在window对象的范围内将Object()作为函数调用。

The second one actually instantiates a new object. 第二个实际上实例化了一个新对象。 It's the one you want to use to create an object. 这是您要用来创建对象的对象。

The difference may not be obvious with the Object() function, but let's say you create your own type, like so: Object()函数的区别可能并不明显,但是让我们说您创建自己的类型,如下所示:

function User(name) {
    this.name = name;
}
var u1 = User("John");
var u2 = new User("Jane");

console.log(u1); // *undefined* because `User()` doesn't return anything.
console.log(this.name); // John
console.log(window.name); // John
console.log(u2.name); // "Jane"

The Object function itself is a special case--it does create a new Object. Object函数本身是一个特例-它确实创建了一个新的Object。 But since most functions don't work that way, it's good to get in the habit of using the new keyword when instantiating things. 但是由于大多数功能都无法正常工作,因此最好在实例化时养成使用new关键字的习惯。 If you're just creating a plain old Object , on the other hand, most people prefer the more concise syntax: 另一方面,如果您只是创建一个普通的旧Object ,那么大多数人都喜欢更简洁的语法:

var myObj = {};

The first statement is a function call, meaning that myObj will get whatever is returned in the Object() function. 第一条语句是函数调用,这意味着myObj将获得Object()函数中返回的任何内容。 As it happens, the function Object() will provide you with a reference to an Object object, whereas 'normal' constructors will not. 碰巧的是,函数Object()将为您提供对Object对象的引用,而“普通”构造函数则不会。

See fe the following: 请参阅以下内容:

function O(){
  this.bla= "bla";
  return this;
}

Calling O() here will yield a reference to window , not to an instance of O . 在这里调用O()将产生对window的引用,而不是O的实例。

暂无
暂无

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

相关问题 JavaScript:基于原型创建对象而不使用new + Constructor - JavaScript: Creating objects based on a prototype without using new + Constructor 在不创建新实例的情况下访问构造函数的属性 - Accessing property of constructor without creating new instance 自定义Array的构造函数,或使用自定义构造函数创建新的数组对象? - Customizing an Array's constructor, or creating a new array object with a custom constructor? 为什么即使在使用new关键字创建对象时手动将其更改为另一个构造函数,也要执行A对象的构造函数? - Why constructor of A object is executed even though we change it manually to another constructor while creating objects using new keyword? JavaScript:使用没有运算符'new'的构造函数 - JavaScript: using constructor without operator 'new' 通过创建新的构造函数来拦截构造函数 - Intercept a constructor by creating a new constructor 如何设置Object的构造函数而不更改为新的Object? - How to set the constructor of an Object without changing to a new Object? 使用 new 创建 object 时是否需要从构造函数返回 - Is RETURN from constructor necessary when creating object with new Javascript 构造函数创建与先前创建的具有相同值的新对象 object - Javascript constructor creating new objects with same values as previously created object 是否可以在不使用JS创建对象的情况下调用构造函数函数方法? - Is it possible to call constructor function method without creating object in JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM