1.子构造函数继承父构造函数中的属性
a. 先定义一个父构造函数
b. 再定义一个子构造函数
c. 子构造函数继承父构造函数的属性(使用call方法)
注意:只能继承到属性,继承不到原型上的方法。
function Father(uname, age) {
this.uname = uname;
this.age = age;
}
function Son(uname, age, score) {
Father.call(this, uname, age);
this.score = score;
}
var son = new Son('刘德华', 18, 100);
console.log(son);
2.借用原型对象继承方法
a. 先定义一个父构造函数
b. 再定义一个子构造函数
c. 子构造函数继承父构造函数的属性(使用call方法,call方法不会继承原型上的方法)
function Father(uname, age) {
this.uname = uname;
this.age = age;
}
Father.prototype.money = function() {
console.log(100000);
};
function Son(uname, age, score) {
Father.call(this, uname, age);
this.score = score;
}
Son.prototype = new Father();
Son.prototype.constructor = Son;
Son.prototype.exam = function() {
console.log('孩子要考试');
}
var son = new Son('刘德华', 18, 100);
console.log(son);
3.组合继承就是把上面2种方式结合在一起