javascript继承之原型链(一)

 1  function Father() {
 2             this.fatherValue = "爸爸";
 3         }
 4         Father.prototype.getFatherValue = function () { 
 5             return this.fatherValue
 6         }
 7         function Son() {
 8             this.sonValue = "儿子";
 9         }
10         Son.prototype = new Father();
11         Son.prototype.getSonValue = function () {
12             return this.sonValue;
13         }
14         var xiaoMing = new Son();
15         alert(xiaoMing.getSonValue());

先实例化father,并且把这个对象赋值给son的原型.
son继承了father之后,再修改原型,添加getSonValue()方法.
通过原型来实现继承时,原型实际上会变成另一个类型的实例.于是,原先的实例属性也就顺利成章的变成了现在的原型属性.
这里存在一个问题,子类son无法向父类father中传参.这个问题会在下一章的借用构造函数继承中有所优化.

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