prototype 변천

3840 단어 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) 
            */          

    좋은 웹페이지 즐겨찾기