Object.getOwnPropertyDescriptors() 方法用来获取一个对象的所有自身属性的描述符。

语法

Object.getOwnPropertyDescriptors(obj)

参数

obj
任意对象

返回值

所指定对象的所有自身属性的描述符,如果没有任何自身属性,则返回空对象。

示例

浅拷贝一个对象

Object.assign() 方法只能拷贝源对象的可枚举的自身属性,同时拷贝时无法拷贝属性的特性们,而且访问器属性会被转换成数据属性,也无法拷贝源对象的原型,该方法配合 Object.create() 方法可以实现上面说的这些。

Object.create(
  Object.getPrototypeOf(obj), 
  Object.getOwnPropertyDescriptors(obj) 
);

创建子类

创建子类的典型方法是定义子类,将其原型设置为超类的实例,然后在该实例上定义属性。这么写很不优雅,特别是对于 getters 和 setter 而言。 相反,您可以使用此代码设置原型:

function superclass() {}
superclass.prototype = {
  // 在这里定义方法和属性
};
function subclass() {}
subclass.prototype = Object.create(superclass.prototype, Object.getOwnPropertyDescriptors({
  // 在这里定义方法和属性
}));

规范

Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
Object.getOwnPropertyDescriptors
Draft Initial definition in ECMAScript 2017.
ECMAScript 2017 (ECMA-262)
Object.getOwnPropertyDescriptors
Standard  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
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.

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
User must explicitly enable this feature.
User must explicitly enable this feature.
 

相关链接