javascript继承之组合继承(三)

 1 function Father(name) {
 2             this.name = name;            
 3         }
 4         Father.prototype.say = function () {
 5             return this.name;
 6         }
 7         function Son(name, age) {
 8             Father.call(this, name);
 9             this.age = age;
10         }
11         Son.prototype = new Father();
12         /*因为constructor来自于原型,而son的原型是被father实例重写了,
13         所以son原型中的constructor是来自father的原型,而father中的原型中的constructor指向father.
14         所以Son.prototype.constructor需要重置成son.
15         这一部可以根据实际情况来确定是否重置
16         */
17         Son.prototype.constructor = Son;        
18         var s = new Son("李世明", 23);
19         alert(s.say()); //李世明
20         alert(s.age);//23

组合式继承避免了原型链和借用构造函数的缺陷,融合了他们的有点,
成文javascript中最常用的继承模式.

原文地址:https://www.cnblogs.com/guoyansi19900907/p/3595636.html