• prototype、__proto__、constructor 的作用与区别


    *以下全文用[[Prototype]]代表对象真正的原型链

      prototype__proto__的区别

    • __proto__是真正的原型链,也就是[[Prototype]],使用原型链时,查找对象的属性是通过__proto__来查找,而不是prototype
      1. varFoo=function(){};
        Foo.__proto__.a ='b';
        Foo.prototype.a ='a';
        alert(Foo.a);//b
    • prototype只是用于构建用new关键字创建的对象,将构造器函数的prototype的引用指向于构建的对象的__proto__
      1. varFoo=function(){};
        var test =newFoo();
        alert(test.__proto__ ===Foo.prototype);//true
    • 构造器函数的prototype__proto__是不相同的,其protoype会被构建的对象的[[Prototype]]引用,其[[Prototype]]Function对象的[[Prototype]]Function对象的prototype__proto__是相同的
      1. varFoo=function(){};
        alert(Foo.prototype ===Foo.__proto__);//false
        alert(Function.__proto__ ===Foo.__proto__);//true
        alert(Function.__proto__ ===Function.prototype);//true

      constructor是什么

      constructor也就是构造器函数的引用,一个构造器函数的prototypeconstructor也就等于这个构造器函数本身,一个构造器函数的__proto__constructor,也就是Functionconsturctor就等于Function本身
      1. varFoo=function(){};
        var test =newFoo();
        alert(Foo.constructor ===Function);//true
        alert(Foo.prototype.constructor === test.constructor);//true
        alert(Foo=== test.constructor);//true
        alert(Foo.constructor ===Foo.prototype.constructor);// false

      一个对象的构造过程

      1. Foo
         |_ protoype
              |_ consturctor  === Foo
              |_ __proto__ === Object.prototype
                  |_ constuctor === Object
                  |_ __proto__ === null
         |_ __proto__
             |_ __proto__ === Function.protoype
                  |_ constuctor === Function
                       |_ __proto__ === Object.protoype
                           |_ consturctor === Object
                           |_ __proto__ === null
        test
            |_ __proto__ === Foo.protoype
                |_ constructor === Foo
                |_ __proto__ === Object.protoype
                    |_ constuctor === Object
                    |_ __proto__ === null
      只有函数对象才有protoype属性,Object.__proto__ === Function.__proto__Object.__proto__.contructor === Object

         enter image description here
      





  • 相关阅读:
    Oracle EBS R12 (12.1.3) Installation Linux(64 bit)
    LINUX的磁盘管理du命令详解
    Solution for Error FRM-92095: Oracle Jnitiator version too low
    EBS learning history (to know about the environment)
    MD5 Checksums for R12.1.1 Rapid Install Media (文档 ID 802195.1)
    adpatch options=hotpatch
    B树和B+树
    Elasticsearch倒排索引
    mysql主从复制
    Found option without preceding group in config file /etc/mysql/my.cnf
  • 原文地址:https://www.cnblogs.com/me10zyl/p/5549215.html
Copyright © 2020-2023  润新知