0
我已经创建了一个执行操作的工作对象。为了保护它,同时做两个动作,有可能表明一个动作完成,工人准备下一个就绪标志...如下面的代码...Javascript工作对象没有正确设置标志
var worker = {
readyFlag:1,
ready: function(){
console.log('ready');
this.readyFlag = 1;
},
working: function(){
console.log('working');
this.readyFlag = 0;
},
work: function() {
console.log(this.readyFlag);
if(this.readyFlag == 1){
this.working();
doTheWork(this.ready);
}
},
};
function doTheWork(callback){
// do the work
console.log('I am executing some work');
callback();
}
worker.work();
worker.work();
问题是,它首次运行正常,它将标志设置为准备就绪..但由于某种原因,它将标志设置回工作,即使.working()函数未被调用,并且console.log也不会显示它再次工作..
请注意,worker.work()被调用两次,但第二次,它不会执行,因为该标志设置为0,即使回调函数应该设置它到1.发生了什么事?
http://jsbin.com/wijimuriga/edit?html,js,console,output
而不是将'readyFlag'设置为'1'或'0',我会使用'true'和'false'。 – AMACB