美文网首页
_proto_ & prototype

_proto_ & prototype

作者: 茂茂爱吃鱼 | 来源:发表于2018-03-24 22:23 被阅读0次

    prototype: 函数的固有属性,是一个对象,对象中有一个固定属性constructor指向该函数

    prototype.png

    __proto__: Object的固有属性,故所有对象均具有该属性,指向创建该对象的函数的prototype

    proto.png
    函数也是对象,那么问题来了,Object/Function/Array/String/Number/Boolean/Date/RegExp/Error的__proto__指向什么呢?

    由于函数是通过new Function()创建的,那么结果就应该是Function.prototype

    内置对象的__proto__.png
    Function/Array/String/Number/Boolean/Date/RegExp/Error的prototype也是个对象,那么它们的__proto__指向什么呢?

    对象是通过new Object()创建的,那么答案应该是Object.prototype

    内置对象原型的__proto__.png
    Object.prototype.__proto__呢?

    这是个特例,指向null

    Object原型的__proto__.png
    最后说一下instanceof机制
    x instanceof y;
    

    沿着x的__proto__找是否与y沿着prototype一致,若一致返回true,反之为false

    Function instanceof Function;    // true
    Function instanceof Object;      // true
    

    以上表达式等价于下图表达式


    instanceof.png

    最后献上一张神图


    _proto_&prototype.jpg

    相关文章

      网友评论

          本文标题:_proto_ & prototype

          本文链接:https://www.haomeiwen.com/subject/pmnnqftx.html