zoukankan      html  css  js  c++  java
  • prototype演变

    setp1

    
        var Person = function () {};  //构造器
        var p = new Person();
    

    setp1 演变:

         var Person = function () {};
         var p = new Person();  
          /*
          ==>   p.__proto__ = Person.prototype  ==> Person.call(p);
          */
    

    setp1 演变证明

        var Person = function () {};
        var p = new Person();
        alert(p.__proto__=== Person.prototype)     //true
    
    
    • __proto__是内部原型,prototype是构造器原型(构造器其实就是函数)
    • 所有构造器/函数的__proto__都指向Function.prototype,它是一个空函数(Empty function)
    • Function.prototype.__proto__ === Object.prototype 与此同时 Object.prototype.__proto__ === null

    setp2

         var Person = function () {};  //构造器
         Person.prototype.Say = function () {           //原型方法
            alert("Person say")
         }
         var p = new Person();
         p.Say();
    

    setp2 演变:

          var Person = function () {};  //构造器
          Person.prototype.Say = function () {           //原型方法
             alert("Person say")
          }
          var p = new Person();  
          /*
           ==>   p.__proto__ = Person.prototype     ==>    Person.call(p);
          */
          p.Say();             
          /*
          ==>   p.Say() (找不到!)    ==>   p.__proto__.Say() (找到了) 
          */
    

    setp2 演变证明:

    
            var Person = function () {};
            var p = new Person();
            alert(p.Say=== Person.prototype.Say)     //true
    
    

    step3

    
            var Person = function () {};       //构造器
            Person.prototype.Say = function () {     //原型方法
                 alert("Person say");
            }
            Person.prototype.Salary = 50000;     //原型属性
            
            var Programmer = function () { };      //构造器
            Programmer.prototype = new Person();    
            Programmer.prototype.WriteCode = function () {   //原型方法
                alert("programmer writes code");
            };
            Programmer.prototype.Salary = 500;     //原型属性
            
            var p = new Programmer();
            p.Say();
            p.WriteCode();
            alert(p.Salary);
      
    
    

    step3演变:

    
            var Person = function () {};       //构造器
            Person.prototype.Say = function () {     //原型方法
                 alert("Person say");
            }
            Person.prototype.Salary = 50000;     //原型属性
            
            var Programmer = function () { };      //构造器
            Programmer.prototype = new Person();    
            /*
             ==》 Programmer.prototype = p1 ( var p1 = new Person() )  
                    //这里 : p1.__proto__ ==  Person.prototype 
             ==>  Programmer.prototype.__proto__ == p1.__proto__ ==  Person.prototype
             ==》 Programmer.prototype.__proto__ = Person.prototype;
            */
            Programmer.prototype.WriteCode = function () {   //原型方法
                alert("programmer writes code");
            };
            Programmer.prototype.Salary = 500;     //原型属性
            
            var p = new Programmer();
            /*
                ==》p.__proto__ = Programmer.prototype ;       
               ==》 p.__proto__.__proto__ = Person.prototype     
            */
            p.Say();
            /*
              ==> p.Say()  (没有这个Say)
              ==> p.__proto__.Say()(没有这个Say) 
                // p.__proto__ == Programmer.prototype (没有这个Say)
              ==> p.__proto__.__proto__.Say() (找到了!) 
                // p.__proto__.__proto__ = Person.prototype(这里有Say) 
            */
            p.WriteCode();
            /*
               ==> p.WriteCode()  (没有这个WriteCode)
               ==> p.__proto__.WriteCode  (找到了!) // p.__proto__ == Programmer.prototype(这里有WriteCode) 
            */          
    
  • 相关阅读:
    android加固系列—2.加固前先要学会破解,调试内存值修改程序走向
    算法—12.广度优先搜索
    算法—11.深度优先搜索
    算法—10.红黑二叉查找树
    算法—二叉查找树的相关一些操作及总结
    binary_search
    no title
    be face up to early
    Linux虚拟机网络配置
    网络工程问题历史遗留
  • 原文地址:https://www.cnblogs.com/iyueyao/p/3925270.html
Copyright © 2011-2022 走看看