Object
class represents one of
JavaScript 的数据类型
. It is used to store various keyed collections and more complex entities. Objects can be created using the
Object()
constructor or the
对象初始化器/文字句法
.
Nearly all objects in JavaScript are instances of
Object
; a typical object inherits properties (including methods) from
Object.prototype
, although these properties may be shadowed (a.k.a. overridden). However, an
Object
may be deliberately created for which this is not true (e.g. by
Object.create(null)
), or it may be altered so that this is no longer true (e.g. with
Object.setPrototypeOf
).
Changes to the
Object
prototype object are seen by
all
objects through prototype chaining, unless the properties and methods subject to those changes are overridden further along the prototype chain. This provides a very powerful although potentially dangerous mechanism to override or extend object behavior.
Object
constructor creates an object wrapper for the given value.
null
or
undefined
, it will create and return an empty object.
When called in a non-constructor context,
Object
behaves identically to
new Object()
.
另请参阅 对象初始化器/文字句法 .
There isn't any method in an Object itself to delete its own properties (such as
Map.prototype.delete()
). To do so, one must use the
delete operator
.
Object()
Object
object. It is a wrapper for the given value.
Object.assign()
Copies the values of all enumerable own properties from one or more source objects to a target object.
Object.create()
Creates a new object with the specified prototype object and properties.
Object.defineProperty()
Adds the named property described by a given descriptor to an object.
Object.defineProperties()
Adds the named properties described by the given descriptors to an object.
Object.entries()
[key, value]
pairs of a given object's
own
enumerable string properties.
Object.freeze()
Freezes an object. Other code cannot delete or change its properties.
Object.fromEntries()
[key, value]
pairs. (This is the reverse of
Object.entries
).
Object.getOwnPropertyDescriptor()
Returns a property descriptor for a named property on an object.
Object.getOwnPropertyDescriptors()
Returns an object containing all own property descriptors for an object.
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Returns an array of all symbol properties found directly upon a given object.
Object.getPrototypeOf()
[[Prototype]]
property) of the specified object.
Object.is()
NaN
values (which differs from both Abstract Equality Comparison and Strict Equality Comparison).
Object.isExtensible()
Determines if extending of an object is allowed.
Object.isFrozen()
Determines if an object was frozen.
Object.isSealed()
Determines if an object is sealed.
Object.keys()
Object.preventExtensions()
Prevents any extensions of an object.
Object.seal()
Prevents other code from deleting properties of an object.
Object.setPrototypeOf()
[[Prototype]]
property).
Object.values()
Object.prototype.constructor
Specifies the function that creates an object's prototype.
Object.prototype.__proto__
Points to the object which was used as prototype when the object was instantiated.
Object.prototype.__noSuchMethod__
Allows a function to be defined that will be executed when an undefined object member is called as a method.
Object.prototype.__defineGetter__()
Associates a function with a property that, when accessed, executes that function and returns its return value.
Object.prototype.__defineSetter__()
Associates a function with a property that, when set, executes that function which modifies the property.
Object.prototype.__lookupGetter__()
__defineGetter__()
方法。
Object.prototype.__lookupSetter__()
__defineSetter__()
方法。
Object.prototype.hasOwnProperty()
Returns a boolean indicating whether an object contains the specified property as a direct property of that object and not inherited through the prototype chain.
Object.prototype.isPrototypeOf()
Returns a boolean indicating whether the object this method is called upon is in the prototype chain of the specified object.
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
toString()
.
Object.prototype.toString()
Returns a string representation of the object.
Object.prototype.unwatch()
Removes a watchpoint from a property of the object.
Object.prototype.valueOf()
Returns the primitive value of the specified object.
Object.prototype.watch()
Adds a watchpoint to a property of the object.
Object
given
undefined
and
null
types
The following examples store an empty
Object
object in
o
:
let o = new Object()
let o = new Object(undefined)
let o = new Object(null)
Object
to create
布尔
对象
The following examples store
布尔
objects in
o
:
// equivalent to o = new Boolean(true) let o = new Object(true)
// equivalent to o = new Boolean(false) let o = new Object(Boolean())
When altering the behavior of existing
Object.prototype
methods, consider injecting code by wrapping your extension before or after the existing logic. For example, this (untested) code will pre-conditionally execute custom logic before the built-in logic or someone else's extension is executed.
When a function is called, the arguments to the call are held in the array-like "variable"
arguments
. For example, in the call
myFn(a, b, c)
, the arguments within
myFn
's body will contain 3 array-like elements corresponding to
(a, b, c)
.
When modifying prototypes with hooks, pass
this
and the arguments (the call state) to the current behavior by calling
apply()
on the function. This pattern can be used for any prototype, such as
Node.prototype
,
Function.prototype
,等。
var current = Object.prototype.valueOf;
// Since my property "-prop-value" is cross-cutting and isn't always
// on the same prototype chain, I want to modify Object.prototype:
Object.prototype.valueOf = function() {
if (this.hasOwnProperty('-prop-value')) {
return this['-prop-value'];
} else {
// It doesn't look like one of my objects, so let's fall back on
// the default behavior by reproducing the current behavior as best we can.
// The apply behaves like "super" in some other languages.
// Even though valueOf() doesn't take arguments, some other hook may.
return current.apply(this, arguments);
}
}
Since JavaScript doesn't exactly have sub-class objects, prototype is a useful workaround to make a “base class” object of certain functions that act as objects. For example:
var Person = function(name) {
this.name = name;
this.canTalk = true;
};
Person.prototype.greet = function() {
if (this.canTalk) {
console.log('Hi, I am ' + this.name);
}
};
var Employee = function(name, title) {
Person.call(this, name);
this.title = title;
};
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee; //If you don't set Object.prototype.constructor to Employee,
//it will take prototype.constructor of Person (parent).
//To avoid that, we set the prototype.constructor to Employee (child).
Employee.prototype.greet = function() {
if (this.canTalk) {
console.log('Hi, I am ' + this.name + ', the ' + this.title);
}
};
var Customer = function(name) {
Person.call(this, name);
};
Customer.prototype = Object.create(Person.prototype);
Customer.prototype.constructor = Customer; //If you don't set Object.prototype.constructor to Customer,
//it will take prototype.constructor of Person (parent).
//To avoid that, we set the prototype.constructor to Customer (child).
var Mime = function(name) {
Person.call(this, name);
this.canTalk = false;
};
Mime.prototype = Object.create(Person.prototype);
Mime.prototype.constructor = Mime; //If you don't set Object.prototype.constructor to Mime,
//it will take prototype.constructor of Person (parent).
//To avoid that, we set the prototype.constructor to Mime (child).
var bob = new Employee('Bob', 'Builder');
var joe = new Customer('Joe');
var rg = new Employee('Red Green', 'Handyman');
var mike = new Customer('Mike');
var mime = new Mime('Mime');
bob.greet();
// Hi, I am Bob, the Builder
joe.greet();
// Hi, I am Joe
rg.greet();
// Hi, I am Red Green, the Handyman
mike.greet();
// Hi, I am Mike
mime.greet();
| 规范 |
|---|
|
ECMAScript (ECMA-262)
在该规范中的 Object 定义。 |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Object
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
Object()
构造函数
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
assign
|
Chrome 45 | Edge 12 | Firefox 34 | IE No | Opera 32 | Safari 9 | WebView Android 45 | Chrome Android 45 | Firefox Android 34 | Opera Android 32 | Safari iOS 9 | Samsung Internet Android 5.0 | nodejs 4.0.0 |
构造函数
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
create
|
Chrome 5 | Edge 12 | Firefox 4 | IE 9 | Opera 11.6 | Safari 5 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs Yes |
__defineGetter__
弃用
|
Chrome 1 | Edge 12 |
Firefox
1
|
IE 11 | Opera 9.5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
defineProperties
|
Chrome 5 | Edge 12 | Firefox 4 | IE 9 | Opera 11.6 | Safari 5 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs Yes |
defineProperty
|
Chrome 5 | Edge 12 | Firefox 4 |
IE
9
|
Opera 11.6 |
Safari
5.1
|
WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 |
Safari iOS
6
|
Samsung Internet Android 1.0 | nodejs Yes |
__defineSetter__
弃用
|
Chrome 1 | Edge 12 |
Firefox
1
|
IE 11 | Opera 9.5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
entries
|
Chrome 54 | Edge 14 | Firefox 47 | IE No | Opera 41 | Safari 10.1 | WebView Android 54 | Chrome Android 54 | Firefox Android 47 | Opera Android 41 | Safari iOS 10.3 | Samsung Internet Android 6.0 |
nodejs
7.0.0
|
freeze
|
Chrome 6 | Edge 12 | Firefox 4 | IE 9 | Opera 12 | Safari 5.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 6 | Samsung Internet Android 1.0 | nodejs Yes |
fromEntries
|
Chrome 73 | Edge 79 | Firefox 63 | IE No | Opera 60 | Safari 12.1 | WebView Android 73 | Chrome Android 73 | Firefox Android 63 | Opera Android No | Safari iOS 12.2 | Samsung Internet Android No | nodejs 12.0.0 |
getOwnPropertyDescriptor
|
Chrome 5 | Edge 12 | Firefox 4 |
IE
9
|
Opera 12 | Safari 5 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs Yes |
getOwnPropertyDescriptors
|
Chrome 54 | Edge 15 | Firefox 50 | IE No | Opera 41 | Safari 10 | WebView Android 54 | Chrome Android 54 | Firefox Android 50 | Opera Android 41 | Safari iOS 10 | Samsung Internet Android 6.0 |
nodejs
7.0.0
|
getOwnPropertyNames
|
Chrome 5 | Edge 12 | Firefox 4 | IE 9 | Opera 12 | Safari 5 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs Yes |
getOwnPropertySymbols
|
Chrome 38 | Edge 12 | Firefox 36 | IE No | Opera 25 | Safari 9 | WebView Android 38 | Chrome Android 38 | Firefox Android 36 | Opera Android 25 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
getPrototypeOf
|
Chrome 5 | Edge 12 | Firefox 3.5 | IE 9 | Opera 12.1 | Safari 5 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12.1 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs Yes |
hasOwnProperty
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
is
|
Chrome 30 | Edge 12 | Firefox 22 | IE No | Opera 17 | Safari 9 | WebView Android ≤37 | Chrome Android 30 | Firefox Android 22 | Opera Android 18 | Safari iOS 9 | Samsung Internet Android 2.0 | nodejs 0.10 |
isExtensible
|
Chrome 6 | Edge 12 | Firefox 4 | IE 9 | Opera 12 | Safari 5.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 6 | Samsung Internet Android 1.0 | nodejs Yes |
isFrozen
|
Chrome 6 | Edge 12 | Firefox 4 | IE 9 | Opera 12 | Safari 5.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 6 | Samsung Internet Android 1.0 | nodejs Yes |
isPrototypeOf
|
Chrome 1 | Edge 12 | Firefox 1 | IE 9 | Opera 4 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
isSealed
|
Chrome 6 | Edge 12 | Firefox 4 | IE 9 | Opera 12 | Safari 5.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 6 | Samsung Internet Android 1.0 | nodejs Yes |
keys
|
Chrome 5 | Edge 12 | Firefox 4 | IE 9 | Opera 12 | Safari 5 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 5 | Samsung Internet Android 1.0 | nodejs Yes |
__lookupGetter__
弃用
|
Chrome 1 | Edge 12 | Firefox 1 | IE 11 | Opera 9.5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
__lookupSetter__
弃用
|
Chrome 1 | Edge 12 | Firefox 1 | IE 11 | Opera 9.5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
preventExtensions
|
Chrome 6 | Edge 12 | Firefox 4 | IE 9 | Opera 12 | Safari 5.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 6 | Samsung Internet Android 1.0 | nodejs Yes |
propertyIsEnumerable
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 4 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
__proto__
弃用
|
Chrome 1 | Edge 12 | Firefox 1 | IE 11 | Opera 10.5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 11 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
seal
|
Chrome 6 | Edge 12 | Firefox 4 | IE 9 | Opera 12 | Safari 5.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 6 | Samsung Internet Android 1.0 | nodejs Yes |
setPrototypeOf
|
Chrome 34 | Edge 12 | Firefox 31 | IE 11 | Opera 21 | Safari 9 | WebView Android 37 | Chrome Android 34 | Firefox Android 31 | Opera Android 21 | Safari iOS 9 | Samsung Internet Android 2.0 | nodejs 0.12 |
toLocaleString
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 4 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
toSource
弃用
非标
|
Chrome No | Edge No |
Firefox
1 — 74
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No | Firefox Android 4 | Opera Android No | Safari iOS No | Samsung Internet Android No | nodejs No |
toString()
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
valueOf
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
值
|
Chrome 54 | Edge 14 | Firefox 47 | IE No | Opera 41 | Safari 10.1 | WebView Android 54 | Chrome Android 54 | Firefox Android 47 | Opera Android 41 | Safari iOS 10.3 | Samsung Internet Android 6.0 |
nodejs
7.0.0
|
完整支持
不支持
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
用户必须明确启用此特征。
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function