function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue = function(){ return this.property; }; function SubType(){ this.subproperty = false; } // 继承了 SuperType SubType.prototype = new SuperType(); var subType = new SubType(); console.log(subType.getSuperValue()); // 继承了 SuperType 的 getSuperValue 方法,打印 true
function SuperType(){ this.colors = ["red", "blue", "green"]; } function SubType(){ } // 继承了 SuperType SubType.prototype = new SuperType(); var subType1 = new SubType(); subType1.colors.push("black"); // subType1 修改 colors console.log(subType1.colors); // "red,blue,green,black" var subType2 = new SubType(); console.log(subType2.colors); // "red,blue,green,black",subType2 的 colors 值为 subType1 修改以后的值
function SuperType(){ this.colors = ["red", "blue", "green"]; } function SubType(){ // 继承了 SuperType SuperType.call(this); // 经过 apply() 或 call() 执行 SuperType 的构造函数 } var instance1 = new SubType(); instance1.colors.push("black"); alert(instance1.colors); // "red,blue,green,black" var instance2 = new SubType(); alert(instance2.colors); // "red,blue,green"
该方法解决了 原型链继承
的引用型属性共享的问题。javascript
还有能够在子类型构造函数中向超类型构造函数传递参数,以下例子:java
function SuperType(name){ this.name = name; } function SubType(){ // 继承 SuperType,并传递参数 SuperType.call(this, "Nicholas"); // 实例属性 this.age = 29; } var subType = new SubType(); console.log(subType.name); //"Nicholas"; console.log(subType.age); //29
function SuperType(name){ this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function(){ console.log(this.name); }; function SubType(name, age){ // 执行 SuperType 的构造函数,继承 SuperType 的属性 SuperType.call(this, name); this.age = age; } // 将 SuperType 的实例赋给 SubType 的原型对象,这样 SubType 可继承 SuperType 原型中的方法 SubType.prototype = new SuperType(); SubType.prototype.sayAge = function(){ console.log(this.age); }; var subType1 = new SubType("Nicholas", 29); subType1.colors.push("black"); console.log(subType1.colors); // "red,blue,green,black" subType1.sayName(); // "Nicholas"; subType1.sayAge(); // 29 var subType2 = new SubType("Greg", 27); console.log(subType2.colors); // "red,blue,green" subType2.sayName(); // "Greg"; subType2.sayAge(); // 27
该方法基于已有的对象建立新对象,同时还没必要所以建立自定义类型。数组
var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = Object.create(person); // 使用 person 做为新对象(anotherPerson)的原型 anotherPerson.name = "Greg"; anotherPerson.friends.push("Rob"); var yetAnotherPerson = Object.create(person); yetAnotherPerson.name = "Linda"; yetAnotherPerson.friends.push("Barbie"); console.log(person.friends); // "Shelby,Court,Van,Rob,Barbie"
该方法建立一个仅用于封装继承过程的函数,该函数在内部以某种方式来加强对象,最后再像真地是它作了全部工做同样返回对象。app
function creatAnother(original) { var clone = Object.create(original); // 建立一个新对象 clone.sayHi = function() { // 以某种方式来加强这个对象 console.log("hi"); } return clone; // 返回该对象 } // 使用示例 var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = creatAnother(person); anotherPerson.sayHi(); // "hi"
该方法解决原型链/构造函数组合继承调用两次超类型构造函数的问题。函数
经过借用构造函数来继承属性,经过原型链的混成形式来继承方法。其背后的基本思路是:没必要为了指定子类型的原型而调用超类型的构造函数,咱们所须要的无非就是超类型原型的一个副本而已。本质上,就是使用寄生式继承来继承超类型的原型,而后再将结果指定给子类型的原型。this
function object(o){ function F(){} F.prototype = o; return new F(); } function inheritPrototype(subType, superType){ var prototype = object(superType.prototype); // 根据超类型原型建立新对象 prototype.constructor = subType; // 将新对象的 constructor 设置为子类型 subType.prototype = prototype; // 将新对象赋给子类型的原型 } function SuperType(name){ this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function(){ console.log(this.name); }; function SubType(name, age){ SuperType.call(this, name); this.age = age; } inheritPrototype(SubType, SuperType); SubType.prototype.sayAge = function(){ console.log(this.age); }; var subType1 = new SubType("Nicholas", 29); subType1.colors.push("black"); console.log(subType1.colors); // "red,blue,green,black" subType1.sayName(); // "Nicholas"; subType1.sayAge(); // 29 var subType2 = new SubType("Greg", 27); alert(subType2.colors); // "red,blue,green" subType2.sayName(); // "Greg"; subType2.sayAge(); // 27
参考资料:《JavaScript高级程序设计(第3版)》第6.3节 继承prototype