Object.getOwnPropertyDescriptors()
方法用来获取一个对象的所有自身属性的描述符。
语法
Object.getOwnPropertyDescriptors(obj)
参数
obj
- 任意对象
返回值
所指定对象的所有自身属性的描述符,如果没有任何自身属性,则返回空对象。
示例
浅拷贝一个对象
Object.assign()
方法只能拷贝源对象的可枚举的自身属性,同时拷贝时无法拷贝属性的特性们,而且访问器属性会被转换成数据属性,也无法拷贝源对象的原型,该方法配合 Object.create()
方法可以实现上面说的这些。
Object.create( Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj) );
Creating a subclass
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, Object.getOwnPropertyDescriptors({
// define your methods and properties here
}));
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 2017 Draft (ECMA-262) Object.getOwnPropertyDescriptors |
Draft | Initial definition in ES8 / ECMAScript 2017. |
浏览器兼容性
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | ? | 50 (50) | ? | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | 50.0 (50) | ? | ? | ? |