下面这些基于构造函数的创建对象的语法有什么区别:
person = new Object()
...这个文字语法:
person = {
property1 : "Hello"
};
看起来两者都做同样的事情,尽管JSLint更喜欢使用对象文字表示法。
哪个更好,为什么?
下面这些基于构造函数的创建对象的语法有什么区别:
person = new Object()
...这个文字语法:
person = {
property1 : "Hello"
};
看起来两者都做同样的事情,尽管JSLint更喜欢使用对象文字表示法。
哪个更好,为什么?
当前回答
另外,根据O'Really的一些javascript书籍....(引用)
使用字面量而不是Object构造函数的另一个原因是没有作用域解析。因为有可能您已经创建了具有相同名称的局部构造函数,解释器需要从调用Object()的位置一直查找作用域链,直到找到全局Object构造函数。
其他回答
我发现ES6/ES2015有一个不同之处。不能使用简写箭头函数语法返回对象,除非用new object()包围对象。
> [1, 2, 3].map(v => {n: v});
[ undefined, undefined, undefined ]
> [1, 2, 3].map(v => new Object({n: v}));
[ { n: 1 }, { n: 2 }, { n: 3 } ]
这是因为编译器被{}括号搞糊涂了,认为n: i是一个label:语句结构;分号是可选的,所以它不会抱怨。
如果向对象添加另一个属性,它最终会抛出一个错误。
$ node -e "[1, 2, 3].map(v => {n: v, m: v+1});"
[1, 2, 3].map(v => {n: v, m: v+1});
^
SyntaxError: Unexpected token :
对于没有方法的简单对象(如您的示例)没有区别。 然而,当开始向对象添加方法时,就有很大的不同了。
文字:
function Obj( prop ) {
return {
p : prop,
sayHello : function(){ alert(this.p); },
};
}
原型:
function Obj( prop ) {
this.p = prop;
}
Obj.prototype.sayHello = function(){alert(this.p);};
这两种方法都允许像这样创建Obj实例:
var foo = new Obj( "hello" );
然而,使用文字方式,在对象的每个实例中携带sayHello方法的副本。而在原型方法中,方法定义在对象原型中,并在所有对象实例之间共享。 如果有很多对象或方法,文字方式会导致相当大的内存浪费。
这里的每个人都在谈论这两者的相似之处。我会指出它们的不同之处。
Using new Object() allows you to pass another object. The obvious outcome is that the newly created object will be set to the same reference. Here is a sample code: var obj1 = new Object(); obj1.a = 1; var obj2 = new Object(obj1); obj2.a // 1 The usage is not limited to objects as in OOP objects. Other types could be passed to it too. The function will set the type accordingly. For example if we pass integer 1 to it, an object of type number will be created for us. var obj = new Object(1); typeof obj // "number" The object created using the above method (new Object(1)) would be converted to object type if a property is added to it. var obj = new Object(1); typeof obj // "number" obj.a = 2; typeof obj // "object" If the object is a copy of a child class of object, we could add the property without the type conversion. var obj = new Object("foo"); typeof obj // "object" obj === "foo" // true obj.a = 1; obj === "foo" // true obj.a // 1 var str = "foo"; str.a = 1; str.a // undefined
这里有很多很好的答案,但我想用我的50美分来回答。
所有这些答案所缺少的是一个简单的类比,它适用于刚开始学习编程语言的人。
希望我能用这个类比来填补这个空白:
对象文字创建vs基于构造函数的语法
感受句子创作的不同之处。
如果我有一句话“我喜欢奶酪”,我可以清楚响亮地(字面上,或逐字逐句地)告诉你:我喜欢奶酪。
这是我逐字逐句创造的句子。
所有其他的方法都是一些棘手的方法来让你理解我到底创造了什么句子。例如,我告诉你:
在我的句子中,主语是“I”,宾语是“cheese”,谓词是“to like”。 这是你学习同一句话的另一种方式,没有任何歧义:“我喜欢奶酪”。
Or,
我的句子有3个单词:第一个是英语字典里的第n个单词,第二个是英语字典里的第m个单词,最后一个是英语字典里的第l个单词。
在这种情况下,你也会得到同样的结果:你确切地知道这句话是什么。
你可以设计任何其他方法,这将不同于“逐字”的句子创建(字面),这将是间接(非字面,非逐字)的句子创建方法。
我认为这是这里的核心概念。
2019年更新
我在OSX High Sierra 10.13.6节点10.13.0版本上运行了与@rjloura相同的代码,这些是结果
console.log('Testing Array:');
console.time('using[]');
for(var i=0; i<200000000; i++){var arr = []};
console.timeEnd('using[]');
console.time('using new');
for(var i=0; i<200000000; i++){var arr = new Array};
console.timeEnd('using new');
console.log('Testing Object:');
console.time('using{}');
for(var i=0; i<200000000; i++){var obj = {}};
console.timeEnd('using{}');
console.time('using new');
for(var i=0; i<200000000; i++){var obj = new Object};
console.timeEnd('using new');
Testing Array:
using[]: 117.613ms
using new: 117.168ms
Testing Object:
using{}: 117.205ms
using new: 118.644ms