启动脚本app.js如下:
#!/usr/bin/env nodefunction asyncFunction(callback){ setTimeout(callback,200);}var color = 'blue';(function(color){ asyncFunction(function(){ console.log('The color is '+ color); });})(color);color = 'green';
console.log('end...');
启动node服务:node app.js
执行结果打印:
end....
The color is blue
如果不使用闭包,代码如下:
function asyncFunction(callback){ setTimeout(callback,200);}var color = 'blue';asyncFunction(function(){ console.log('The color is '+ color);});color = 'green';
console.log('end...');
启动node服务:node app.js
执行结果打印:
end....
The color is green
新闻热点
疑难解答