标签:
1.原型链
1 function superType() { 2 this.property = true; 3 } 4 5 superType.prototype.getSuperValue = function () { 6 return this.property; 7 } 8 9 function subType() { 10 this.subProperty = false; 11 } 12 13 subType.prototype = new superType(); 14 subType.prototype.getSubValue = function () { 15 return this.subProperty; 16 } 17 18 var instance = new subType(); 19 console.log(instance.getSuperValue());
2.借用构造函数
1 function superType(name) { 2 this.name = name; 3 } 4 5 function subType() { 6 // 继承了超类,同时还传递了参数 7 superType.call(this, ‘Nick‘); 8 this.age = 29; 9 } 10 11 var instance = new subType(); 12 console.log(instance.name);
3.组合继承
1 function superType(name) { 2 this.name = name; 3 this.color = ["red", "blue"]; 4 } 5 6 superType.prototype.sayName = function () { 7 console.lgo(this.name); 8 } 9 10 function subType(name, age) { 11 superType.call(this, name); 12 this.age = age; 13 } 14 // 继承 15 subType.prototype = new superType(); 16 subType.prototype.sayAge = function () { 17 console.log(this.age); 18 } 19 20 var instance = new subType(‘Nick‘, 29); 21 instance.sayAge();
4.原型式继承
1 function object(o) { 2 function F() { }; 3 F.prototype = o; 4 return new F(); 5 } 6 // 这个方法和 Object.create()函数类似,不过后者只兼容主流浏览器 7 var person = { 8 name: "Nick", 9 age: 29 10 } 11 12 var another = Object.create(person); 13 console.log(another.name);
5.寄生
标签:
原文地址:http://www.cnblogs.com/gemicat/p/4817411.html