Javascript对象继承

时间:2015-05-12 07:46:47

标签: javascript inheritance

我正在开展一个项目,我注意到在不同的领域有多个相同对象的引用。但我一直在阅读mixins和原型继承,但不确定要遵循哪一个:

所以我的当前对象如下所示,但我需要产品继承基类,包括每次使用的函数。

var base = function() {
    this.id = 0;
    this.refererer = null;
    this.getCurrency = function() {
        return "US"
    }
}

var product = function() {
    this.name = "";
    this.description = "";
}

如何实现上述功能以使用mixins或原型继承?

1 个答案:

答案 0 :(得分:0)

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"
相关问题