【问题标题】:Javascript objects inheritanceJavascript对象继承
【发布时间】:2015-05-12 07:46:46
【问题描述】:
我正在做一个项目,我注意到在不同区域有多个对相同对象的引用。但我一直在阅读 mixins 和原型继承,但不确定要遵循哪一个:
所以我当前的对象如下所示,但我需要产品继承基类,包括每次使用的函数。
var base = function() {
this.id = 0;
this.refererer = null;
this.getCurrency = function() {
return "US"
}
}
var product = function() {
this.name = "";
this.description = "";
}
如何实现上述以使用 mixins 或原型继承?
【问题讨论】:
标签:
javascript
inheritance
【解决方案1】:
var base = function() {
this.id = 0;
this.refererer = null;
this.getCurrency = function() {
return "US"
}
}
var product = function() {
this.name = "";
this.description = "";
}
product.prototype = new base(); // this will do the inheritance trick
product.prototype.constructor = product;
var proObj = new product();
alert(proObj.id); // 0 base class property "id"