Object.getOwnPropertyDescriptor() 方法返回指定对象上一个自有属性对应的属性描述符。
(自有属性指的是直接赋予该对象的属性,不需要从原型链上进行查找的属性)

  1. const object1 = {
  2. property1: 42
  3. };
  4. const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
  5. console.log(descriptor1.configurable);
  6. // expected output: true
  7. console.log(descriptor1.value);
  8. // expected output: 42

描述

该方法允许对一个属性的描述进行检索。在 Javascript 中, 属性 由一个字符串类型的“名字”(name)和一个“属性描述符”(property descriptor)对象构成。更多关于属性描述符类型以及他们属性的信息可以查看:Object.defineProperty.
一个属性描述符是一个记录,由下面属性当中的某些组成的:
value
该属性的值(仅针对数据属性描述符有效)
writable
当且仅当属性的值可以被改变时为true。(仅针对数据属性描述有效)
get
获取该属性的访问器函数(getter)。如果没有访问器, 该值为undefined。(仅针对包含访问器或设置器的属性描述有效)
set
获取该属性的设置器函数(setter)。 如果没有设置器, 该值为undefined。(仅针对包含访问器或设置器的属性描述有效)
configurable
当且仅当指定对象的属性描述可以被改变或者属性可被删除时,为true。
enumerable
当且仅当指定对象的属性可以被枚举出时,为 true。

  1. var o, d;
  2. o = { get foo() { return 17; } };
  3. d = Object.getOwnPropertyDescriptor(o, "foo");
  4. // d {
  5. // configurable: true,
  6. // enumerable: true,
  7. // get: /*the getter function*/,
  8. // set: undefined
  9. // }
  10. o = { bar: 42 };
  11. d = Object.getOwnPropertyDescriptor(o, "bar");
  12. // d {
  13. // configurable: true,
  14. // enumerable: true,
  15. // value: 42,
  16. // writable: true
  17. // }
  18. o = {};
  19. Object.defineProperty(o, "baz", {
  20. value: 8675309,
  21. writable: false,
  22. enumerable: false
  23. });
  24. d = Object.getOwnPropertyDescriptor(o, "baz");
  25. // d {
  26. // value: 8675309,
  27. // writable: false,
  28. // enumerable: false,
  29. // configurable: false
  30. // }

Object.getOwnPropertyDescriptors()

code:

  1. const object1 = {
  2. property1 : 42 ,
  3. property2 : 2
  4. };
  5. const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
  6. console.log(Object.entries(descriptor1))
  7. var dess = Object.getOwnPropertyDescriptors(object1 )
  8. console.log(Object.entries(dess))

print:

  1. [
  2. [ 'value', 42 ],
  3. [ 'writable', true ],
  4. [ 'enumerable', true ],
  5. [ 'configurable', true ]
  6. ]
  7. [
  8. [
  9. 'property1',
  10. { value: 42, writable: true, enumerable: true, configurable: true }
  11. ],
  12. [
  13. 'property2',
  14. { value: 2, writable: true, enumerable: true, configurable: true }
  15. ]

从打印中我们可以看到他拥有所有的

3. Create() + Object.getOwnPropertyDescriptors()实现对象浅拷贝

回顾Create+assign实现混合继承的代码:

  1. function MyClass() {
  2. SuperClass.call(this);
  3. OtherSuperClass.call(this);
  4. }
  5. // 继承一个类
  6. MyClass.prototype = Object.create(SuperClass.prototype);
  7. // 混合其它
  8. Object.assign(MyClass.prototype, OtherSuperClass.prototype);
  9. // 重新指定constructor
  10. MyClass.prototype.constructor = MyClass;
  11. MyClass.prototype.myMethod = function() {
  12. // do a thing
  13. };

问题:Object.assign(MyClass.prototype, OtherSuperClass.prototype)

Object.assign() 方法只能拷贝源对象的可枚举的自身属性,同时拷贝时无法拷贝属性的特性们,而且访问器属性会被转换成数据属性,也无法拷贝源对象的原型,该

因此当我们要的是拷贝对象的时候就应该使用:

  1. Object.create(
  2. Object.getPrototypeOf(obj),
  3. Object.getOwnPropertyDescriptors(obj)
  4. );

4. 优雅创建子类的方式

  1. function superclass() {}
  2. superclass.prototype = {
  3. // 在这里定义方法和属性
  4. };
  5. function subclass() {}
  6. subclass.prototype = Object.create(superclass.prototype, Object.getOwnPropertyDescriptors({
  7. // 在这里定义方法和属性
  8. }));