Object

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

语法

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

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

参数

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

描述

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

当以非构造函数形式被调用时,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.__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()
给对象的某个属性增加监听。
在指定对象为上下文情况下执行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
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Object Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
assign Chrome Full support 45 Edge Full support 12 Firefox Full support 34 IE No support No Opera Full support 32 Safari Full support 9 WebView Android Full support 45 Chrome Android Full support 45 Firefox Android Full support 34 Opera Android Full support 32 Safari iOS Full support Yes Samsung Internet Android Full support 5.0 nodejs Full support 4.0.0
constructor Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 4 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
__count__
Deprecated Non-standard
Chrome No support No Edge No support No Firefox No support No IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support No Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
create Chrome Full support 5 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 11.6 Safari Full support 5 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support 12 Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
__defineGetter__
Deprecated
Chrome Full support 1 Edge Full support 12 Firefox 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 11 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
defineProperties Chrome Full support 5 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 11.6 Safari Full support 5 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support 12 Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
defineProperty Chrome Full support 5 Edge Full support 12 Firefox Full support 4 IE 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.6 Safari 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 Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support 12 Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
__defineSetter__
Deprecated
Chrome Full support 1 Edge Full support 12 Firefox 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 11 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
entries Chrome Full support 54 Edge Full support 14 Firefox Full support 47 IE No support No Opera Full support 41 Safari Full support 10.1 WebView Android Full support 54 Chrome Android Full support 54 Firefox Android Full support 47 Opera Android Full support 41 Safari iOS Full support 10.3 Samsung Internet Android Full support 6.0 nodejs 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.
eval
Deprecated Non-standard
Chrome No support No Edge No support No Firefox No support No IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support No Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
freeze Chrome Full support 6 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 12 Safari Full support 5.1 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
fromEntries Chrome Full support 73 Edge No support No Firefox Full support 63 IE No support No Opera Full support 60 Safari Full support 12.1 WebView Android Full support 73 Chrome Android Full support 73 Firefox Android Full support 63 Opera Android No support No Safari iOS Full support 12.2 Samsung Internet Android No support No nodejs Full support 12.0.0
getNotifier
Deprecated Non-standard
Chrome No support 36 — 52 Edge No support No Firefox No support No IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support No Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
getOwnPropertyDescriptor Chrome Full support 5 Edge Full support 12 Firefox Full support 4 IE Full support 8 Opera Full support 12 Safari Full support 5 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
getOwnPropertyDescriptors Chrome Full support 54 Edge Full support 15 Firefox Full support 50 IE No support No Opera Full support 41 Safari Full support 10 WebView Android Full support 54 Chrome Android Full support 54 Firefox Android Full support 50 Opera Android ? Safari iOS ? Samsung Internet Android Full support 6.0 nodejs 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.
getOwnPropertyNames Chrome Full support 5 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 12 Safari Full support 5 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
getOwnPropertySymbols Chrome Full support 38 Edge Full support 12 Firefox Full support 36 IE No support No Opera Full support 25 Safari Full support 9 WebView Android Full support 38 Chrome Android Full support 38 Firefox Android Full support 36 Opera Android Full support 25 Safari iOS Full support 9 Samsung Internet Android Full support 3.0 nodejs Full support 0.12
getPrototypeOf Chrome Full support 5 Edge Full support 12 Firefox Full support 3.5 IE Full support 9 Opera Full support 12.1 Safari Full support 5 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
hasOwnProperty Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 5.5 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
is Chrome Full support 30 Edge Full support 12 Firefox Full support 22 IE No support No Opera Full support Yes Safari Full support 9 WebView Android Full support Yes Chrome Android Full support 30 Firefox Android Full support 22 Opera Android Full support Yes Safari iOS Full support 9 Samsung Internet Android Full support Yes nodejs Full support 0.10
isExtensible Chrome Full support 6 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 12 Safari Full support 5.1 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
isFrozen Chrome Full support 6 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 12 Safari Full support 5.1 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
isPrototypeOf Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 9 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
isSealed Chrome Full support 6 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 12 Safari Full support 5.1 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
keys Chrome Full support 5 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 12 Safari Full support 5 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
__lookupGetter__
Deprecated
Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 11 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
__lookupSetter__
Deprecated
Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 11 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
__noSuchMethod__
Deprecated Non-standard
Chrome No support No Edge No support No Firefox No support 1 — 43 IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support 4 — 43 Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
observe
Deprecated Non-standard
Chrome No support 36 — 52 Edge No support No Firefox No support No IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support No Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
__parent__
Deprecated Non-standard
Chrome No support No Edge No support No Firefox No support No IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support No Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
preventExtensions Chrome Full support 6 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 12 Safari Full support 5.1 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
propertyIsEnumerable Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 5.5 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
__proto__
Deprecated
Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 11 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
prototype Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
seal Chrome Full support 6 Edge Full support 12 Firefox Full support 4 IE Full support 9 Opera Full support 12 Safari Full support 5.1 WebView Android Full support Yes Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
setPrototypeOf Chrome Full support 34 Edge Full support 12 Firefox Full support 31 IE Full support 11 Opera Full support Yes Safari Full support 9 WebView Android Full support 37 Chrome Android Full support 34 Firefox Android Full support 31 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support 0.12
toLocaleString Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 5.5 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
toSource
Deprecated Non-standard
Chrome No support No Edge No support No Firefox Full support 1 IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android Full support 4 Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
toString Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
unobserve
Deprecated Non-standard
Chrome No support 36 — 52 Edge No support No Firefox No support No IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support No Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
unwatch
Deprecated Non-standard
Chrome No support No Edge No support No Firefox No support 1 — 58 IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support 4 — 58 Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
valueOf Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 4 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
values Chrome Full support 54 Edge Full support 14 Firefox Full support 47 IE No support No Opera Full support 41 Safari Full support 10.1 WebView Android Full support 54 Chrome Android Full support 54 Firefox Android Full support 47 Opera Android Full support 41 Safari iOS Full support 10.3 Samsung Internet Android Full support 6.0 nodejs 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.
watch
Deprecated Non-standard
Chrome No support No Edge No support No Firefox No support 1 — 58 IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support 4 — 58 Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No

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.

相关链接