Last updated 4 years ago
Was this helpful?
將函式作為參數傳遞, 借由這個特性我們可以抽離一部份的商業邏輯, 將這個商務邏輯放到函式參數中, 可以分離程式碼中變化與不變的部份
1.使用時機
1.將function當成參數代入另一個function中
2.當要接續某個function執行完後,再執行另一個function, 例如ajax
2.架構
ref: http://fireqqtw.logdown.com/posts/258823-javascript-function-notes
function two(callback) { var count1 = 1; (callback && typeof(callback) === "function") && callback(); console.log(count1); } function three() { console.log('three run'); } two(function() { console.log('callback run'); }); two(three);
3.範例
1.https://codepen.io/JenHsuan/pen/qrYYgB?editors=0011
2.currying