Object 构造函数创建一个对象包装器。
语法
// 对象初始化器(Object initialiser)或对象字面量(literal)
{ [ nameValuePair1[, nameValuePair2[, ...nameValuePairN] ] ] }
// 以构造函数形式来调用
new Object([value])
参数
nameValuePair1, nameValuePair2, ... nameValuePairN- 成对的名称(字符串)与值(任何值),其中名称通过冒号与值分隔。
value- 任何值。
描述
在JavaScript中,几乎所有的对象都是Object类型的实例,它们都会从Object.prototype继承属性和方法。Object 构造函数为给定值创建一个对象包装器。Object构造函数,会根据给定的参数创建对象,具体有以下情况:
- 如果给定值是
null或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.__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字符串代码,已经废弃。
示例
给定 undefined 和 null 类型使用 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 (ECMA-262) Object |
Living Standard | Added Object.entries and Object.values. |
浏览器兼容
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Object | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Object() constructor | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | 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 9 | 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 4 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
__defineGetter__ | Chrome Full support 1 | Edge Full support 12 | Firefox
Full support
1
| IE Full support 11 | Opera Full support 9.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | 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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
defineProperty | 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.1
| WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS
Full support
6
| Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
__defineSetter__ | Chrome Full support 1 | Edge Full support 12 | Firefox
Full support
1
| IE Full support 11 | Opera Full support 9.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | 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
|
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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
fromEntries | Chrome Full support 73 | Edge Full support 79 | 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 |
getOwnPropertyDescriptor | 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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 | 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 Full support 41 | Safari iOS Full support 10 | Samsung Internet Android Full support 6.0 | nodejs
Full support
7.0.0
|
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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 | 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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12.1 | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 | 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 5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | 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 17 | Safari Full support 9 | WebView Android Full support ≤37 | Chrome Android Full support 30 | Firefox Android Full support 22 | Opera Android Full support 18 | Safari iOS Full support 9 | Samsung Internet Android Full support 2.0 | 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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | 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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
isPrototypeOf | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 9 | Opera Full support 4 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | 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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | 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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 5 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
__lookupGetter__ | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 11 | Opera Full support 9.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
__lookupSetter__ | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 11 | Opera Full support 9.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | 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 4 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
__proto__ | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 11 | Opera Full support 10.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 11 | Safari iOS Full support 1 | 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 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
setPrototypeOf | Chrome Full support 34 | Edge Full support 12 | Firefox Full support 31 | IE Full support 11 | Opera Full support 21 | Safari Full support 9 | WebView Android Full support 37 | Chrome Android Full support 34 | Firefox Android Full support 31 | Opera Android Full support 21 | Safari iOS Full support 9 | Samsung Internet Android Full support 2.0 | 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 4 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toSource | Chrome No support No | Edge No support No | Firefox
No support
1 — 74
| 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 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
valueOf | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | 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
|
Legend
- Full support
- Full support
- No support
- No support
- 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.