Object.getOwnPropertyDescriptors()
method returns all own property descriptors of a given object.
Object.getOwnPropertyDescriptors(obj)
obj
The object for which to get all own property descriptors.
An object containing all own property descriptors of an object. Might be an empty object, if there are no properties.
This method permits examination of the precise description of all own properties of an object. A
property
in JavaScript consists of either a string-valued name or a
Symbol
and a property descriptor. Further information about property descriptor types and their attributes can be found in
Object.defineProperty()
.
A property descriptor is a record with some of the following attributes:
value
The value associated with the property (data descriptors only).
writable
true
if and only if the value associated with the property may be changed (data descriptors only).
get
undefined
if there is no getter (accessor descriptors only).
set
undefined
if there is no setter (accessor descriptors only).
configurable
true
if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
enumerable
true
if and only if this property shows up during enumeration of the properties on the corresponding object.
Whereas the
Object.assign()
method will only copy enumerable and own properties from a source object to a target object, you are able to use this method and
Object.create()
for a shallow copy between two unknown objects:
Object.create( Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj) );
A typical way of creating a subclass is to define the subclass, set its prototype to an instance of the superclass, and then define properties on that instance. This can get awkward especially for getters and setters. Instead, you can use this code to set the prototype:
function superclass() {}
superclass.prototype = {
// Define your methods and properties here
};
function subclass() {}
subclass.prototype = Object.create(
superclass.prototype,
{
// Define your methods and properties here
}
);
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.getOwnPropertyDescriptors' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
|
完整支持
不支持
用户必须明确启用此特征。
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