【发布时间】:2012-09-04 16:58:17
【问题描述】:
我正在阅读有关mixin pattern in javascript 的内容,并且遇到了这段我不明白的代码:
SuperHero.prototype = Object.create( Person.prototype );
原来的代码实际上有一个错字(大写的H)。如果我小写它,它会起作用。但是,如果我真的删除该行,一切似乎都一样。
这里是完整的代码:
var Person = function( firstName , lastName ){
this.firstName = firstName;
this.lastName = lastName;
this.gender = "male";
};
// a new instance of Person can then easily be created as follows:
var clark = new Person( "Clark" , "Kent" );
// Define a subclass constructor for for "Superhero":
var Superhero = function( firstName, lastName , powers ){
// Invoke the superclass constructor on the new object
// then use .call() to invoke the constructor as a method of
// the object to be initialized.
Person.call( this, firstName, lastName );
// Finally, store their powers, a new array of traits not found in a normal "Person"
this.powers = powers;
};
SuperHero.prototype = Object.create( Person.prototype );
var superman = new Superhero( "Clark" ,"Kent" , ["flight","heat-vision"] );
console.log( superman );
// Outputs Person attributes as well as powers
SuperHero.prototype = Object.create( Person.prototype ); 是做什么的?
【问题讨论】:
标签: javascript