我是node.js / javascript的新手,来自C#world:可能我的问题是将C#习惯转换为javascript ...
我创建了一个小型node.js应用程序来重现我遇到的问题。 在应用程序中,两个孩子'被创造了。他们每个人都会抓住一个气球,并使其以或多或少的力量(参数)反弹。当气球停止结束时,我们会从一个事件中获取一个日志..
我遇到的问题是,第一个被诅咒的孩子似乎被最后一个碾压了。暗恋似乎发生在对“反弹”的呼唤上。功能
这是我期待的日志序列:
- 迈克到了花园。小孩想玩气球......
- Elia到了花园。小孩想玩气球......
- 确保Mike的名字是Mike ......
- 确保Elia的名字是Elia ......
- 迈克刚拿到一个气球并在上面写下了他/她的名字!
- 迈克让气球反弹(强迫10)!
- Elia刚拿到一个气球并在上面写下了他/她的名字!
- Elia让气球反弹(强制4)!
- Elia的气球已经停止弹跳......上面写着名字是Elia。
醇>10.Mike的气球已经停止弹跳......上面写着的名字是迈克。
这是我得到的日志序列:
- 迈克到了花园。小孩想玩气球......
- Elia到了花园。小孩想玩气球......
- 确保Mike的名字是Mike ......
- 确保Elia的名字是Elia ......
- Elia刚拿到一个气球并在上面写下了他/她的名字!
- Elia使气球反弹(强制10)!
- Elia刚拿到一个气球并在上面写下了他/她的名字!
- Elia让气球反弹(强制4)!
- Elia的气球已经停止弹跳......上面写着名字是Elia。
醇>10. Elia的气球已停止弹跳......上面写着名字是Elia 。
构成应用程序的三个文件。
app.js
const Kid = require('./kid.js');
var mike = new Kid('Mike');
var elia = new Kid('Elia');
console.log(`Ensure that Mike's name is ${mike.name}...`);
console.log(`Ensure that Elia's name is ${elia.name}...`);
mike.bounce(10);
elia.bounce(4);
kid.js
const Balloon = require('./balloon.js');
var self;
function Kid(name){
self=this;
this.name=name;
console.log(`${this.name} arrived in the garden. The kid wants to play with a balloon....`);
}
Kid.prototype.bounce = function(force1To10) {
var balloon = new Balloon(self.name);
balloon.bounce(force1To10);
balloon.on('balloonStopped',(kidnameWrittenOnIt)=>{
console.log(`${self.name}'s balloon has stopped bouncing... The name written on it is ${kidnameWrittenOnIt}.`);
});
};
module.exports=Kid;
balloon.js
const timers = require("timers");
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var self;
function Balloon(kidName) {
self = this;
this.kidName = kidName;
this.bounce = function(kidsPower1To10){
console.log(`${self.kidName} makes the balloon bounce (force ${kidsPower1To10}) !`);
timers.setTimeout(() => {
self.emit('balloonStopped',self.kidName);
}, kidsPower1To10*1000);
}
console.log(`${this.kidName} just got a balloon and wrote his/her name on it !`);
}
inherits(Balloon, EventEmitter);
module.exports = Balloon;
执行:
node app.js
感谢提前获取任何有用的信息!
答案 0 :(得分:0)
确实问题出在bounce
方法中。您引用了一个全局self
变量,该变量将引用第二个人。因此,如果您在第一个人上拨打bounce
,它仍会引用第二个人。
解决方案是直接使用this
代替self
,或者在每种方法中定义self
:
function Kid(name){
this.name=name;
console.log(`${this.name} arrived in the garden. The kid wants to play with a balloon....`);
}
Kid.prototype.bounce = function(force1To10) {
var balloon = new Balloon(this.name);
balloon.bounce(force1To10);
balloon.on('balloonStopped',(kidnameWrittenOnIt)=>{
console.log(`${this.name}'s balloon has stopped bouncing... The name written on it is ${kidnameWrittenOnIt}.`);
});
};
现在,您仍需要注意,在引用bounce
方法时,请确保this
已正确绑定。当您调用mike.bounce(10)
或elia.bounce(2)
时会出现这种情况,但是当您开始将bounce
方法分配给另一个变量并调用其他变量时会有陷阱。在这种情况下,this
将是未定义的。
您还可以使用class
语法对上述内容进行编码:
class Kid {
constructor(name) {
this.name=name;
console.log(`${this.name} arrived in the garden. The kid wants to play with a balloon....`);
}
bounce(force1To10) {
var balloon = new Balloon(this.name);
balloon.bounce(force1To10);
balloon.on('balloonStopped',(kidnameWrittenOnIt)=>{
console.log(`${this.name}'s balloon has stopped bouncing... The name written on it is ${kidnameWrittenOnIt}.`);
});
}
}