1. // class 实际上是函数,可见的语法糖
  2. typeof People // 'function'
  3. typeof Student // 'function'
  4. // 隐式原型和显示原型
  5. console.log(xialuo.__proto__)
  6. console.log(Student.prototype)
  7. console.log(xialuo.__proto__ === Student.prototype)

Snipaste_2020-07-19_21-32-58.png

  • 每个class都有显示原型prototype
  • 每个实例都有隐式原型proto
  • 实例的proto指向对应的class的prototype

基于原型的执行规则

获取属性xialuo.name或执行方法xialuo.sayHi()时

  1. 现在自身属性和方法寻找
  2. 如果找不到则自动去proto中查找