javascript point to problems in the constructor

First, use an example pointed to the form constructor.

function Fruit () {}
 var F = new new Fruit (); 
the console.log (f.constructor); // Print out Fruit ()

From the above code we conclude Conclusion 1: The above code can be seen in the console constructor is a pointer to the constructor Fruit references.

function Fruit () = {this.name "fruit"}
 // var Fruit new new F = (); 
function the Apple () = {this.name "apple";} 
Apple.prototype = new new Fruit ();
 var Apple = new new the Apple (); 
the console.log (apple.constructor); // still print out Fruit () 
Apple.prototype = {}; // constructor empty object is object () 
Apple = new new the Apple (); 
the console.log ( apple.constructor); // point Object ()

This place is a bit strange. The constructor in the end point is the constructor of the instance?

The above code summed Conclusion 2: constructor is directed constructor a reference to the prototype object

   apple.constructor==Apple.prototype.constructor

var apple2=new apple.constructor();
console.log(apple2.name);

or

var apple2=new Apple.prototype.constructor();
console.log(apple2.name);

Printing is fruit.

We now want him to do is Apple's print; this time need to be reassigned to point constructor.

The above two conclusions: either modified example of the constructor or a constructor prototype constructor object properties are achieved;

Such code can write code after overwriting prototype property

Apple.prototype.constructor=Apple;

Or for instance constructor will be re-enacted after instantiation.

 apple.constructor=Apple;

 

Guess you like

Origin www.cnblogs.com/guoyansi19900907/p/12089151.html