“{}”之间的差异和“new Object()” [英] Difference between "{}" and "new Object()"

查看:166
本文介绍了“{}”之间的差异和“new Object()”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

创建对象 - 新对象或对象文字符号?

以下内容之间究竟有什么区别:

What exactly is the difference between the following:

var myData = new Object();
myData["name"] = "ATOzTOA";
myData["site"] = "atoztoa";

var myData = {};
myData["name"] = "ATOzTOA";
myData["site"] = "atoztoa";

更新

我得到的是这个......

What I got is this...

var myData = {
    "name" : "ATOzTOA",
    "site" : "atoztoa",
};

var myData = new Object({
    "name" : "ATOzTOA",
    "site" : "atoztoa",
});

我是对的吗?

推荐答案

没有区别(技术上)。 {} 只是新对象()快捷方式

There is no difference (technically). {} is just a shortcut for new Object().

但是,如果指定对象文字,则可以直接形成具有多个属性的新对象。

However, if you assign an object literal, you may directly form a new object with multiple properties.

var myData = {
    name:   'ATOzTOA',
    size:   'atoztoa'
};

..这可能会更方便。此外,它减少了对象的访问,并最终更快。但这是关于微观优化的。更重要的是打字要少得多。

..which might feel more convenient. Also, it reduces the access on the object and is ultimately faster. But that is about microoptimizations. More important is that its a lot less to type.

这篇关于“{}”之间的差异和“new Object()”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆