1

这是我的代码:

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

Quo.prototype.get_status = function() { //This gives all instances of Quo the 'get_status' method, 
                                        //which returns 'this.status' by default, unless another 
                                        //instance rewrites the return statement.
    return this.status;
};

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

document.write(myQuo);

当我运行此代码时,结果是[object Object]. 由于get_status()附加到Quo prototype,调用 的实例不Quo应该足以调用方法吗?我在这里错过了什么?

4

1 回答 1

2

不应该document.write(myQuo.get_status());吗?

更新:

另一种选择是像这样覆盖 toString 方法:

Quo.prototype.toString = function() {
    return this.status;
};
于 2011-08-04T16:18:40.410 回答