Node.js"无法读取未定义的属性'原型'erroe - Node.js,MongoDB和Angularjs书

cwm*_*ken 5 javascript mongodb node.js angularjs

嘿家伙们希望我不会因犯明显错误而受到抨击.我正在研究Brad Dayley的Node.js,MongoDB和Angularjs一书,我坚持他的一个练习(清单4.4).我有一个简单的脚本emitterListener.js,如下脚本旨在对帐户进行检查.

var events = require('events');
function Account() {
    this.balance = 0;
    events.EventEmitter.call(this);
    this.deposit = function(amount) {
        this.balance += amount;
        this.emit('balanceChanged');
    };
    this.withdraw = function(amount) {
        this.balance -= amount;
        this.emit('balanceChanged');
    };
}
Account.prototype._proto_ = events.EventEmitter.prototype;

function displayBalance() {
    console.log("Account balance: $%d", this.balance);
}

function checkOverdraw() {
    if (this.balance < 0) {
        console.log("Account Overdrawn!!!!!");
    }
}

function checkGoal(acc, goal) {
    if (acc.balance > goal) {
        console.log("Goal Achieved!!!!!");
    }
}
var account = new Account();

account.on("balanceChanged", displayBalance);
account.on("balanceChanged", checkOverdraw);
account.on("balanceChanged", function() {
    checkGoal(this, 1000);
});
account.deposit(220);
account.deposit(320);
account.deposit(600);
account.withdraw(1200);
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我得到了错误.

TypeError: Object #<Account> has no method 'on'
at Object.<anonymous> (/Users/506132/web/emitterListener.js:35:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Run Code Online (Sandbox Code Playgroud)

根据我在研究后的有限理解,我认为这意味着"on"模块没有被加载.我找到了一个解决方案,建议类似于将其添加到第1行

var events = require('events').on;
Run Code Online (Sandbox Code Playgroud)

然后导致错误.

TypeError: Cannot read property 'EventEmitter' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:16:35)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Run Code Online (Sandbox Code Playgroud)

按照第一个修复的逻辑,我尝试使用EventEmitter实现相同的修复

var events = require('events').EventEmitter;
Run Code Online (Sandbox Code Playgroud)

万岁看起来好像......或者不行......现在我得到了这个错误

TypeError: Cannot read property 'prototype' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:17:48)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Run Code Online (Sandbox Code Playgroud)

我尝试添加下面的代码思考为什么不呢?

var events = require('events').prototype;
Run Code Online (Sandbox Code Playgroud)

它只是让我回到以前的错误

TypeError: Cannot read property 'EventEmitter' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:16:35)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?我该如何调试这个以及我应该在哪里看?在此先感谢帮助新手.

干杯.

小智 7

process.EventEmitter已弃用,请require('events')改用.

搜索所有参考文件(应该在多个文件中):

var EventEmitter = process.EventEmitter
Run Code Online (Sandbox Code Playgroud)

它存在的地方,将该行更改为:

var EventEmitter = require('events')
Run Code Online (Sandbox Code Playgroud)


HMR*_*HMR 1

我会将其作为答案发布,这样这个问题就不会被标记为未回答。

你应该改变:

Account.prototype.__proto__ = events.EventEmitter.prototype;
Run Code Online (Sandbox Code Playgroud)

到:

Account.prototype = Object.create(events.EventEmitter.prototype);
Run Code Online (Sandbox Code Playgroud)