How do “Object()” and “new Object()” differ in JavaScript?

独自空忆成欢 提交于 2019-12-31 11:49:26

问题


In JavaScript, what's the difference between

var x = Object();

and

var x = new Object();

?


回答1:


This is pulled directly from the ECMAScript specification:

15.2.1 The Object Constructor Called as a Function

When Object is called as a function rather than as a constructor, it performs a type conversion.

15.2.1.1 Object ( [ value ] )

When the Object function is called with no arguments or with one argument value, the following steps are taken:

  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).

    In Short: new Object([ value ])

  2. Return ToObject(value).

Notes:

[ ] Is A common way to mark a parameter as optional.

ToObject Is a very simple operation that is defined in section 9.9.



来源:https://stackoverflow.com/questions/3080655/how-do-object-and-new-object-differ-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!