首页 > Web开发 > 详细

javascript继承之组合继承(三)

时间:2014-03-12 16:18:16      阅读:410      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 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
bubuko.com,布布扣

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

javascript继承之组合继承(三),布布扣,bubuko.com

javascript继承之组合继承(三)

原文:http://www.cnblogs.com/guoyansi19900907/p/3595636.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!