And the relationship between the prototype and the instance constructors

First, the difference between the ordinary and the constructor function: as shown in FIG.

  1. The first letter of the constructor function name to distinguish between capital and normal function
  2. The constructor is called by new
  3. Constructor constructor this point and this point in the common window functions
. 1          var Dog = function () {                        // ordinary function definition 
2              the console.log ( "I'm an ordinary function" );
 . 3          }
 . 4          Dog ();                                       // ordinary function call 
. 5          
. 6          // ///// ////////////////////////Dividing line//////////////////////// //////////// 
. 7          var Dog = function () {                         // constructor definition 
. 8              the console.log ( "I am a constructor" );
 . 9          }
 10          var Dog = new newDog ();                         // constructor call

 

Second, the prototype constructor: We can console.log (Dog.prototype); to see this prototype constructor Dog

  1. When we have no other means to add Dog, we find that the output console {constructor: ƒ}

  The constructor is every constructor method has the output that we found this constructor this constructor to a Dog

. 1          the console.log (Dog.prototype);                  // Output: {constructor: ƒ} 
2          the console.log (Dog.prototype.constructor);       // to Dog constructor   

  Output below

 

 

  2. When we Adding methods and then view the prototype and constructor by Dog.prototype

. 1          Dog.prototype.newStyle = function () {
 2              the console.log ( "my method of adding a new prototype by Dog" );
 . 3          }   
 . 4  
. 5          the console.log (Dog.prototype);                      // {NewStyle: ƒ, constructor : ƒ} 
. 6          the console.log (Dog.prototype.constructor);          // to Dog constructor

  Console output below, we will find that at this time the Dog prototype (prototype) much of this new method added, constructor and had the same

  From this we can learn prototype constructor mount included in the body of the method and prototype constructors themselves (constructor)

  

 

 Third, the instance constructor

  Examples of the above code is the dog Dog

   And you can have multiple instances but their contents are the same. All point to the constructor

  How to determine if an instance is not part of a constructor

. 1          the console.log (dog the instanceof Dog);    // to true // See example is not part of the constructor dog Dog 
2          the console.log (dog3 the instanceof Dog);    // flase

 

  

Guess you like

Origin www.cnblogs.com/wyctf/p/11487566.html