原型和原型链
class实现继承
constructor
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
|
class实际上是函数,是语法糖 typeof People //'function'
原型
instanceof-类型判断
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
[] instanceof Array [] instanceof Object {} instanceof Object
|
原型关系
- 每个class都有一个显式原型prototype
- 每个实例都有隐式原型
__proto__