Object.getOwnPropertyDescriptors()

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

语法

Object.getOwnPropertyDescriptors(obj)

参数

obj
任意对象

返回值

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

示例

浅拷贝一个对象

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
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
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.

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.
 

相关链接