标签:
The basic idea is to use prototype chaining to inherit properties and methods on the prototype and to use constructor stealing to inherit instance properties. This allows function reuse by defineing methods on the prototype and allows each instance to have its own properties. Consider the following:
function SuperType(name){ this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function(){ alert(this.name); }; function SubType(name, age){ SuperType.call(this, name); this.age = age; } SubType.prototype = new SuperType(); SubType.prototype.sayAge = function(){ alert(this.age); }; var instance1 = new SubType("Nicholas", 29); instance1.color.push("black"); alert(instance1.colors); // "red, blue, green, black"; instance1.sayName(); // "Nicholas"; instance1.sayAge(); // 29; var instance2 = new SubType("Greg", 27); alert(instance2.colors); // "red, blue, green"; instance2.sayName(); // "Greg"; instance2.sayAge(); // 27
标签:
原文地址:http://www.cnblogs.com/linxd/p/4488381.html