接口继承只继承方法签名,而实现继承则继承实际的方法。如前所述,由于函数没有签名,
在 ECMAScript 中无法实现接口继承。ECMAScript 只支持实现继承,而且其实现继承主要是依靠原型链
来实现的。
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
浙公网安备 33010602011771号