Присоединение открытого метода к прототипу - PullRequest
1 голос
/ 04 августа 2011

Это мой код:

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 для вызова метода? Что я здесь пропустил?

1 Ответ

2 голосов
/ 04 августа 2011

Разве это не должно быть document.write(myQuo.get_status());?

Обновление:

Другой вариант - переписать метод toString следующим образом:

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