JavaScript inheritance - inherited prototype chain

// prototype chain inheritance 
function Supertype () {
     the this .name = 'Super' ;
     the this .girlFriends = [ "Xiaoli", "Xiaowang" ]; 
} 
SuperType.prototype.sayName = function () { 
    the console.log ( the this .name ); 
} 
function SubType () {
     the this .age 20 is = ; 
} 
// Create an instance of SuperType assigns SubType prototype 
// implementation inheritance essentially rewriting prototype object, replacing it with a new instance of the type 
SubType.prototype = new new Supertype (); 
SubType.prototype.sayAge = function () { 
    the console.log (the this .age); 
} 
var SUB1 = new new SubType (); 
sub1.sayName ();         // Super 
sub1.sayAge ();         // 20 is 
// now sub.constructor point Supertype 
// because the original is re-constructor is SubType write sake 
console.log (sub1.constructor);     // Supertype function output 

// prototype chain problems 
// the following code, we will find all the instances will share this girlFriends SubType attribute 
// Supertype constructor will be All instances of the shared SubType 
var SUB2 = new new SubType (); 
sub1.girlFriends.push ( "Xiaochen" ); 
the console.log (sub2.girlFriends);     //(3) ["xiaoli", "xiaowang", "xiaochen"]

 

Guess you like

Origin www.cnblogs.com/gehaoyu/p/11804272.html