原型和原型链

class实现继承

constructor

  • extendssuper
//父类
class People {
constructor(name){
this.name = name
}
Chilema(){
console.log(`${this.name}chile`)
}
}

//子类
class Student extends People{
constructor(name, number){
super(name) //继承自父类
this.number = number
}
sayHi(){
console.log(`${this.name}${this.number}`)
}
}

let xiaoming = new Student('xiaoming', 001)

//隐式原型和显式原型
xiaoming.__proto__ === Student.prototype //true

class实际上是函数,是语法糖 typeof People //'function'

原型

instanceof-类型判断

  • instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
[] instanceof Array  //true
[] instanceof Object //true
{} instanceof Object //true

原型关系

  • 每个class都有一个显式原型prototype
  • 每个实例都有隐式原型__proto__
Object.__proto__ : null