javascript中的原型继承,它是如何工作的?

时间:2011-08-04 17:11:25

标签: javascript prototype-programming

这是我的代码:

var Quo = function(string) {            //This creates an object with a 'status' property.
    this.status = string;
};
Quo.get_status = function() {
    return this.status;
}
Quo.get_status = function() {
    return this.status;
}

var myQuo = new Quo("confused");        //the `new` statement creates an instance of Quo().

document.write(myQuo.get_status());     //Why doesnt the get_status() method attach to the new instance of Quo?

当我运行此代码时,结果为[object Object]。我的问题是一个实例继承了构造函数的哪些属性?

1 个答案:

答案 0 :(得分:2)

  

我的问题是实例继承了构造函数的哪些属性?

Quo.prototype中的任何内容都可供该实例使用。

此代码应使您的示例有效:

Quo.prototype.get_status = function() {
    return this.status;
};

Quo.prototype.get_status = function() {
    return this.status;
};

进一步阅读: