prototype prototype understanding

  function A (){
    this.name = "a";
    // this.bat = function(){
    //   alert(this.name)
    // }
  }
  A.prototype.bat = function(){
      alert(this.name)
    }
  function B(){
    A.call(this);
  }

The wording of the above, B inherits the methods and properties of A; B if the method continues to increase, A does not increase

  function A (){
    this.name = "a";
    // this.bat = function(){
    //   alert(this.name)
    // }
  }
  A.prototype.bat = function(){
      alert(this.name)
    }
  function B(){
    A.call(this);
  }
  B.prototype = A.prototype;

The wording of the above, the method B inherited attribute A, while point B the prototype of the prototype A; B if the method continues to increase, A increases automatically

  B.prototype.newbat=function(){
    alert("c");    
  }
  There obj = new A ();
  obj.newbat();
   

In the former, obj no new method; latter, obj contains new method.

 

Guess you like

Origin www.cnblogs.com/xiaoguniang0204/p/12368920.html