javascript继承的几种方式

1次阅读

共计 379 个字符,预计需要花费 1 分钟才能阅读完成。

原型链继承

实现原理 : 重写子类原型对象,代之以一个新类型的实例。

function SuperType(){this.property = true;}
SuperType.prototype.getSuperValue = function(){return this.property;}

function SubType (){this.subproperty = false;}
// 继承了 SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function(){return this.subproperty;}

var instance = new SubType();
alert(instance.getSuperValue()); // true

继承后的实例和构造函数和原型之间的关系图如下:

缺点

正文完
 0