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 |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 ? | Chrome Android Full support 54 | Edge Mobile Full support Yes | Firefox Android Full support 50 | Opera Android ? | Safari iOS ? | Samsung Internet Android Full support 6.0 | nodejs
Full support
7.0.0
|