es6原型的继承

class Parent {
  name = 'liangcheng';
}
const parent = new Parent();
console.log(parent);

// 类继承某实例对象属性
class Child1 extends Parent {
  constructor (obj) {
    super();
    Object.assign(this, obj);
    this.isSelected = false;
  }
}
const child1 = new Child1({name: 'lc'});
console.log('child1----------', child1);
// 继承原型上属性
class Child2 extends Parent {
  age = 0
  constructor (age) {
    super();
    this.age = age;
  }
}
const child2 = new Child2(30);
console.log('child2----------', child2);
  • new.target.name
  • super

猜你喜欢

转载自www.cnblogs.com/liangcheng11/p/9033960.html