// 给click方法做参数的匿名函数就是一个回调函数 $("body").click(function() { alert(`clicked on body`); });
是不是很简单呢?
现在,我们来实现一个回调函数,模拟在游戏中得分升级。
// levelOne()是宿主函数,它接收另外一个函数作为参数 // levelOne()的第二个参数callback是一个回调函数,它的名字可以任意取,通常命名为callback只是为了易于理解 functionlevelOne(value, callback) { var newScore = value + 5; callback(newScore); }
functionstartGame() { var currentScore = 5; console.log('Game Started! Current score is ' + currentScore); // levelOne()的第二个参数为回调函数 levelOne(currentScore, function(levelOneReturnedValue) { console.log('Level One reached! New score is ' + levelOneReturnedValue); }); }
startGame();
执行以上代码,控制台输出是这样的:
"Game Started! Current score is 5" "Level One reached! New score is 10"
有输出可知,levelOne()内的代码(var newScore = value + 5;)执行之后,才会执行回调函数中的代码(console.log(‘Level One reached! New score is ‘ + levelOneReturnedValue);)。
functionlevelOne(value, callback) { var newScore = value + 5; callback(newScore); }
functionlevelTwo(value, callback) { var newScore = value + 10; callback(newScore); }
functionlevelThree(value, callback) { var newScore = value + 30; callback(newScore); }
functionstartGame() { var currentScore = 5; console.log('Game Started! Current score is ' + currentScore); levelOne(currentScore, function(levelOneReturnedValue) { console.log('Level One reached! New score is ' + levelOneReturnedValue); levelTwo(levelOneReturnedValue, function(levelTwoReturnedValue) { console.log('Level Two reached! New score is ' + levelTwoReturnedValue); levelThree(levelTwoReturnedValue, function(levelThreeReturnedValue) { console.log('Level Three reached! New score is ' + levelThreeReturnedValue); }); }); });
}
startGame();
执行以上代码,控制台输出是这样的:
"Game Started! Current score is 5" "Level One reached! New score is 10" "Level Two reached! New score is 20" "Level Three reached! New score is 50"
// 当一切正常时,调用resolve函数;否则调用reject函数 var promise = newPromise(function(resolve, reject) { if ( /* everything turned out fine */ ) { resolve("Stuff worked!"); } else { reject(Error("It broke")); } });
我们将前文陷入回调地狱的例子使用Promise改写:
functionlevelOne(value) { var promise, newScore = value + 5; return promise = newPromise(function(resolve) { resolve(newScore); }); }
functionlevelTwo(value) { var promise, newScore = value + 10; return promise = newPromise(function(resolve) { resolve(newScore); }); }
functionlevelThree(value) { var promise, newScore = value + 30; return promise = newPromise(function(resolve) { resolve(newScore); }); }
var startGame = newPromise(function(resolve, reject) { var currentScore = 5; console.log('Game Started! Current score is ' + currentScore); resolve(currentScore); });
// startGame返回的结果传递给了then函数,然后传递给了levelOne函数 startGame.then(levelOne) .then(function(result) { // result为levelOne函数的返回值 console.log('You have reached Level One! New score is ' + result); return result; }) .then(levelTwo) .then(function(result) { console.log('You have reached Level Two! New score is ' + result); return result; }) .then(levelThree) .then(function(result) { console.log('You have reached Level Three! New score is ' + result); });
执行以上代码,控制台输出还是这样的:
"Game Started! Current score is 5" "Level One reached! New score is 10" "Level Two reached! New score is 20" "Level Three reached! New score is 50"