JS基础-原型和原型链

92 阅读1分钟

如何判断一个变量是不是数组?

  • Array.isArray()
  • a innstanceof Array
  • Object.prototype.toString.call(a) === '[array Object]'

innstanceof 是怎么工作的?

讲解:

class a{
    constructor(){
        this.name = 'xue'
    }
}
a.prototypf 指向原型
b = new a()
b.__proto__ 指向原型
a.prototype === b.__proto__
b.__proto__.__proto__ === Object.prototype

image.png

更具以上图可以写出

function _instanceof(a, b){
    let aProto = a.__proto__
    if(aProto === null) return false;
    if(aProto === b.prototype) return true;
    return _instanceof(aProto, b)
}

class的原型本质, 怎么理解?

image.png

把这个图丰富一下就好, prototype上有哪些属性之类的, 执行规则