10

我有两个模块:apirequest.jsfeed.js。当我调用feed.start()apirequest,我得到TypeError: Object #<Feed> has no method 'start'. 为什么是这样?不util.inherits(Feed, APIRequest);继承 APIRequest 的属性?

apirequest.js

var EventEmitter = require('events').EventEmitter;
var util = require('util');

function APIRequest(endpoint) { } 

APIRequest.prototype.start = function() { }

util.inherits(APIRequest, EventEmitter);

module.exports = APIRequest;

饲料.js

var util = require('util');
var APIRequest = require('../lib/api_request');

function Feed(endpoint) {
  APIRequest.call(this, endpoint);
}

util.inherits(Feed, APIRequest);

var feed = new Feed(endpoint);

feed.start();
4

1 回答 1

13

问题是顺序:

APIRequest.prototype.start = function() { }
util.inherits(APIRequest, EventEmitter); <-- *overwrites* APIRequest.prototype

如果您交换这些行,它将起作用:

util.inherits(APIRequest, EventEmitter);
APIRequest.prototype.start = function() { }
于 2013-03-19T07:10:10.733 回答