var events = require("events");
function CarShow() {
events.EventEmitter.call(this);
this.seeCar = function (make) {
this.emit('sawCar', make);
}
}
CarShow.prototype.__proto__ = events.EventEmitter.prototype;
var show = new CarShow();
function logCar(make) {
console.log("Saw a "+make);
}
function logColorCar(make, color) {
console.log("Saw a %s %s ", color, make);
}
show.on("sawCar", logCar);
show.on("sawCar", function (make) {
var colors = ["red", "blue", "black", "pink", "green"];
var color = colors[Math.floor(Math.random()*3)];
logColorCar(make, color);
});
show.seeCar("Ferrari");
show.seeCar("Porsche");
show.seeCar("Bugatti");
function logCar(logMsg, callback) {
process.nextTick(function () {
callback(logMsg);
});
}
var cars = ["猎豹", "捷达", "朗逸"];
for(var idx in cars){
var msg = "Saw a "+cars[idx];
logCar(msg, function () {
console.log("Normal Callback "+ msg);
});
}
for(var idx in cars){
var msg = "Saw a "+cars[idx];
(function (msg) {
logCar(msg, function () {
console.log("Closure Callback "+ msg);
})
})(msg);
}
//Normal Callback Saw a 朗逸
//Normal Callback Saw a 朗逸
//Normal Callback Saw a 朗逸
//Closure Callback Saw a 猎豹
//Closure Callback Saw a 捷达
//Closure Callback Saw a 朗逸
function logCar(car, callback) {
console.log("Saw a %$", car);
if(cars.length){
process.nextTick(function () {
callback();
});
}
}
function logCars(cars) {
var car = cars.pop();
logCar(car, function () {
logCars(cars);
});
}
var cars = ["猎豹", "捷达", "朗逸"];
logCars(cars);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有