Object 构造函数创建一个对象包装器。

语法

// 对象初始化器(Object initialiser)或对象字面量(literal)
{[nameValuePair1[, nameValuePair2[, ...nameValuePairN]]]} 

// 以构造函数形式来调用
new Object([value])

参数

nameValuePair1, nameValuePair2, ... nameValuePairN
成对的名称(字符串)与值(任何值),其中名称通过冒号与值分隔。
value
任何值。

描述

Object构造函数为给定值创建一个对象包装器。如果给定值是 nullundefined,将会创建并返回一个空对象,否则,将返回一个与给定值对应类型的对象。

当以非构造函数形式被调用时,Object 等同于 new Object()

可查看 对象初始化/字面量语法

Object构造函数的属性

Object.length
值为1。
Object.prototype
可以为所有 Object 类型的对象添加属性。

Object构造函数的方法

Object.assign()
通过复制一个或多个对象来创建一个新的对象。
Object.create()
使用指定的原型对象和属性创建一个新对象。
Object.defineProperty()
给对象添加一个属性并指定该属性的配置。
Object.defineProperties()
给对象添加多个属性并分别指定它们的配置。
Object.entries()
返回给定对象自身可枚举属性的[key, value]数组。
Object.freeze()
冻结对象:其他代码不能删除或更改任何属性。
Object.getOwnPropertyDescriptor()
返回对象指定的属性配置。
Object.getOwnPropertyNames()
返回一个数组,它包含了指定对象所有的可枚举或不可枚举的属性名。
Object.getOwnPropertySymbols()
返回一个数组,它包含了指定对象自身所有的符号属性。
Object.getPrototypeOf()
返回指定对象的原型对象。
Object.is()
比较两个值是否相同。所有 NaN 值都相等(这与==和===不同)。
Object.isExtensible()
判断对象是否可扩展。
Object.isFrozen()
判断对象是否已经冻结。
Object.isSealed()
判断对象是否已经密封。
Object.keys()
返回一个包含所有给定对象自身可枚举属性名称的数组。
Object.preventExtensions()
防止对象的任何扩展。
Object.seal()
防止其他代码删除对象的属性。
Object.setPrototypeOf()
设置对象的原型(即内部[[Prototype]]属性)。
Object.values()
返回给定对象自身可枚举值的数组。

Object 实例和Object 原型对象

JavaScript中的所有对象都来自Object;所有对象从Object.prototype继承方法和属性,尽管它们可能被覆盖。例如,其他构造函数的原型将覆盖constructor属性并提供自己的toString()方法。Object原型对象的更改将传播到所有对象,除非受到这些更改的属性和方法将沿原型链进一步覆盖。

属性

Object.prototype.constructor
特定的函数,用于创建一个对象的原型。
Object.prototype.__proto__
指向当对象被实例化的时候,用作原型的对象。
Object.prototype.__noSuchMethod__
当未定义的对象成员被调用作方法的时候,允许定义并执行的函数。
Object.prototype.__count__
用于直接返回用户定义的对象中可数的属性的数量。已被废除。
Object.prototype.__parent__
用于指向对象的内容。已被废除。

方法

Object.prototype.__defineGetter__()
关联一个函数到一个属性。访问该函数时,执行该函数并返回其返回值。
Object.prototype.__defineSetter__()
关联一个函数到一个属性。设置该函数时,执行该修改属性的函数。
Object.prototype.__lookupGetter__()
返回使用 __defineGetter__ 定义的方法函数 。
Object.prototype.__lookupSetter__()
返回使用 __defineSetter__ 定义的方法函数。
Object.prototype.hasOwnProperty()
返回一个布尔值 ,表示某个对象是否含有指定的属性,而且此属性非原型链继承的。
Object.prototype.isPrototypeOf()
返回一个布尔值,表示指定的对象是否在本对象的原型链中。
Object.prototype.propertyIsEnumerable()
判断指定属性是否可枚举,内部属性设置参见 ECMAScript [[Enumerable]] attribute
Object.prototype.toSource()
返回字符串表示此对象的源代码形式,可以使用此字符串生成一个新的相同的对象。
Object.prototype.toLocaleString()
直接调用 toString()方法。
Object.prototype.toString()
返回对象的字符串表示。
Object.prototype.unwatch()
移除对象某个属性的监听。
Object.prototype.valueOf()
返回指定对象的原始值。
Object.prototype.watch()
给对象的某个属性增加监听。
Object.prototype.eval()
在指定对象为上下文情况下执行javascript字符串代码,已经废弃。

示例

给定 undefinednull 类型使用 Object

下面的例子将一个空的 Object 对象存到 o 中:

var o = new Object();
var o = new Object(undefined);
var o = new Object(null);

使用 Object 生成布尔对象

下面的例子将Boolean 对象存到 o 中:

// 等价于 o = new Boolean(true);
var o = new Object(true);
// 等价于 o = new Boolean(false);
var o = new Object(Boolean());

规范

Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.0.
ECMAScript 5.1 (ECMA-262)
Object
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
Object
Standard Added Object.assign, Object.getOwnPropertySymbols, Object.setPrototypeOf, Object.is
ECMAScript Latest Draft (ECMA-262)
Object
Draft Added Object.entries and Object.values.

浏览器兼容

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
prototypeChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
__count__
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
__noSuchMethod__
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 1 — 43IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support 4 — 43Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
__parent__
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
__proto__
Deprecated
Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support 11Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
constructorChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
assignChrome Full support 45Edge Full support YesFirefox Full support 34IE No support NoOpera Full support 32Safari Full support 9WebView Android No support NoChrome Android Full support 45Edge Mobile Full support YesFirefox Android Full support 34Opera Android No support NoSafari iOS Full support YesSamsung Internet Android Full support 5.0nodejs Full support 4.0.0
createChrome Full support 5Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 11.6Safari Full support 5WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
definePropertiesChrome Full support 5Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 11.6Safari Full support 5WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
definePropertyChrome Full support 5Edge Full support YesFirefox Full support 4IE Full support 9
Notes
Full support 9
Notes
Notes Also supported in Internet Explorer 8, but only on DOM objects and with some non-standard behaviors.
Opera Full support 11.6Safari Full support 5.1
Notes
Full support 5.1
Notes
Notes Also supported in Safari 5, but not on DOM objects.
WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
entriesChrome Full support 54Edge Full support 14Firefox Full support 47IE No support NoOpera Full support 41Safari Full support 10.1WebView Android Full support 54Chrome Android Full support 54Edge Mobile Full support YesFirefox Android Full support 47Opera Android Full support 41Safari iOS Full support 10.1Samsung Internet Android Full support 6.0nodejs Full support 7.0.0
Full support 7.0.0
Full support 6.5.0
Disabled
Disabled From version 6.5.0: this feature is behind the --harmony runtime flag.
freezeChrome Full support 6Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
fromEntriesChrome No support NoEdge No support NoFirefox Full support 63IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support 63Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
getNotifier
DeprecatedNon-standard
Chrome No support 36 — 52Edge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
getOwnPropertyDescriptorChrome Full support 5Edge Full support YesFirefox Full support 4IE Full support 8Opera Full support 12Safari Full support 5WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
getOwnPropertyDescriptorsChrome Full support 54Edge Full support 15Firefox Full support 50IE No support NoOpera Full support 41Safari Full support 10WebView Android ? Chrome Android Full support 54Edge Mobile Full support YesFirefox Android Full support 50Opera Android ? Safari iOS ? Samsung Internet Android Full support 6.0nodejs Full support 7.0.0
Full support 7.0.0
Full support 6.5.0
Disabled
Disabled From version 6.5.0: this feature is behind the --harmony runtime flag.
getOwnPropertyNamesChrome Full support 5Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
getOwnPropertySymbolsChrome Full support 38Edge Full support YesFirefox Full support 36IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support YesFirefox Android Full support 36Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
getPrototypeOfChrome Full support 5Edge Full support YesFirefox Full support 3.5IE Full support 9Opera Full support 12.1Safari Full support 5WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
isChrome Full support 30Edge Full support YesFirefox Full support 22IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 22Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.10
isExtensibleChrome Full support 6Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
isFrozenChrome Full support 6Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
isSealedChrome Full support 6Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
keysChrome Full support 5Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
observe
DeprecatedNon-standard
Chrome No support 36 — 52Edge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
preventExtensionsChrome Full support 6Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
__defineGetter__
Deprecated
Chrome Full support YesEdge Full support YesFirefox Full support 1
Notes
Full support 1
Notes
Notes Starting with Firefox 48, this method can no longer be called at the global scope without any object. A TypeError will be thrown otherwise. Previously, the global object was used in these cases automatically, but this is no longer the case.
IE Full support 11Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
__defineSetter__
Deprecated
Chrome Full support YesEdge Full support YesFirefox Full support 1
Notes
Full support 1
Notes
Notes Starting with Firefox 48, this method can no longer be called at the global scope without any object. A TypeError will be thrown otherwise. Previously, the global object was used in these cases automatically, but this is no longer the case.
IE Full support 11Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
__lookupGetter__
Deprecated
Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support 11Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
__lookupSetter__
Deprecated
Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support 11Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
eval
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
hasOwnPropertyChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
isPrototypeOfChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
propertyIsEnumerableChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
toLocaleStringChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
toSource
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox Full support 1IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support 4Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
toStringChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
unwatch
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 1 — 58IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support 4 — 58Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
valueOfChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
watch
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 1 — 58IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support 4 — 58Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
sealChrome Full support 6Edge Full support YesFirefox Full support 4IE Full support 9Opera Full support 12Safari Full support 5.1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
setPrototypeOfChrome Full support 34Edge Full support YesFirefox Full support 31IE Full support 11Opera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 31Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.12
unobserve
DeprecatedNon-standard
Chrome No support 36 — 52Edge No support NoFirefox No support NoIE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
valuesChrome Full support 54Edge Full support 14Firefox Full support 47IE No support NoOpera Full support 41Safari Full support 10.1WebView Android Full support 54Chrome Android Full support 54Edge Mobile Full support YesFirefox Android Full support 47Opera Android Full support 41Safari iOS Full support 10.3Samsung Internet Android Full support 6.0nodejs Full support 7.0.0
Full support 7.0.0
Full support 6.5.0
Disabled
Disabled From version 6.5.0: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

相关连接

 

文档标签和贡献者

最后编辑者: zhangchen,