您的位置:首页 > Web前端 > JavaScript

javascript继承之组合继承(三)

2014-03-12 09:40 316 查看
function Father(name) {
this.name = name;
}
Father.prototype.say = function () {
return this.name;
}
function Son(name, age) {
Father.call(this, name);
this.age = age;
}
Son.prototype = new Father();
/*因为constructor来自于原型,而son的原型是被father实例重写了,
所以son原型中的constructor是来自father的原型,而father中的原型中的constructor指向father.
所以Son.prototype.constructor需要重置成son.
这一部可以根据实际情况来确定是否重置
*/
Son.prototype.constructor = Son;
var s = new Son("李世明", 23);
alert(s.say()); //李世明
alert(s.age);//23


组合式继承避免了原型链和借用构造函数的缺陷,融合了他们的有点,
成文javascript中最常用的继承模式.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: