node.js中的继承问题:尝试覆盖子类的原型时的赋值错误

时间:2014-10-02 01:47:25

标签: javascript node.js inheritance

谢谢你花时间帮我解决。我正在尝试学习node.js并且在理解继承时遇到了以下问题:

var stream = require('stream');
var util = require('util');

util.inherits(Answers, stream.Readable);

function Answers(opt) {
    stream.Readable.call(this, opt);
    this.quotes = ["yes", "negatory", "possibly"];
    this._index = 0;
}

Answers.prototype._read() = function() {
    if (this._index > this.quotes.length) {
        this.push(null);
    }
    else {
        this.push(this.quotes[this._index]);
        this._index += 1;
    }
};

我的错误表明我的左侧分配无效,我试图覆盖stream.Readable的原型(第12行)。我以为是打电话给

util.inherits(Answers, stream.Readable);

允许我重写stream.Readable的_read()函数。任何帮助将非常感谢。提前谢谢!

1 个答案:

答案 0 :(得分:3)

Answers.prototype._read() ...您正在为函数调用分配值。只需将其更改为Answers.prototype._read = function() ...