我花了1个小时在谷歌上搜索,但仍未找到一个好的答案。所以这是我的问题:如何继承一个类及其原型?
我目前有这个解决方案:http://jsfiddle.net/RdxYN/2/
function BaseContent(a, b) {
this.propertyA = 'propertyA';
this.a = a;
this.b = b;
alert('x');
}
BaseContent.prototype.funcA = function () {
alert(this.a + ', ' + this.b);
alert(this.propertyA);
};
function ContentA(a, b) {
BaseContent.call(this, a, b);
this.funcA();
}
ContentA.prototype = new BaseContent;
ContentA.prototype.constructor = ContentA;
ContentA.prototype.parent = BaseContent.prototype;
var Content = new ContentA('c', 'd');
唯一的问题是,BaseContent会被执行两次。我不希望这样。是否有更好的解决方案或修复方法?
if (!Object.create) Object.create = (function (Factory) { return function (prototype) { Factory.prototype = prototype; return new Factory; }; }(function () {}));
- Aadit M Shah