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());でしょうか?

アップデート:

もう 1 つのオプションは、次のように toString メソッドを上書きすることです。

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