类的 prototype 属性和__proto__属性,类原型链有两条继承路线

类的两天继承路线:类作为一个对象的__proto__属性;Class 作为构造函数的语法糖,同时有prototype属性和__proto__属性;

(1)子类的__proto__属性,表示构造函数的继承,总是指向父类。
(2)子类prototype属性的__proto__属性,表示方法的继承,总是指向父类的prototype属性。

class Person {
    
    
  constructor(name, sex) {
    
    
    this.name = name;
    this.sex = sex;
  }
  say() {
    
    
    return ('lalalala' + this.name)
  }
}
Person.color = 'red'

class PersonOther extends Person {
    
    
  constructor(x,y) {
    
    
    super(x,y)
  }
}

console.log(PersonOther.__proto__ === Person)  // true    // 原型链形成
console.log(PersonOther.prototype.__proto__ === Person.prototype) // true     // 原型链形成

console.log(PersonOther.color) // red   类集成静态属性是不给实例使用的,只是类与类之间静态属性实现了继承

let kate = new PersonOther('kate', 'woman')
console.log(kate.name, kate.sex)  // kate woman

console.log(kate.color)   // undefined
console.log(kate.say())   // lalalala kate

猜你喜欢

转载自blog.csdn.net/weixin_43131046/article/details/125215057