怎么做“对象()”和“new Object()” JavaScript有什么不同? [英] How do "Object()" and "new Object()" differ in JavaScript?
问题描述
在JavaScript中,有什么区别
In JavaScript, what's the difference between
var x = Object();
和
var x = new Object();
?
推荐答案
这是直接从 ECMAScript中提取的规范:
调用Object时作为函数而不是构造函数,它执行类型转换。
When Object is called as a function rather than as a constructor, it performs a type conversion.
当调用Object函数时没有参数或一个参数值时,以下步骤是
:
When the Object function is called with no arguments or with one argument value, the following steps are taken:
-
如果value为null,未定义或不提供
,则创建并返回一个新的
Object对象,就像调用
标准内置Object
构造函数一样与
相同的参数(15.2.2.1)。
If value is null, undefined or not supplied, create and return a new Object object exactly as if the standard built-in Object constructor had been called with the same arguments (15.2.2.1).
简而言之: 新对象([值])
返回ToObject(值)。
Return ToObject(value).
注意:
[] 是将参数标记为可选的常用方法。
Notes:
[ ] Is A common way to mark a parameter as optional.
ToObject 这是一个非常简单的操作,在第9.9节中定义。
ToObject Is a very simple operation that is defined in section 9.9.
这篇关于怎么做“对象()”和“new Object()” JavaScript有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!