將參數傳送到 callback 函式
預設情況下,你不能傳送參數給 callback 函式。例如:
function callback() {
console.log('Hi human');
}
document.getElementById('someelem').addEventListener('click', callback);
你可以利用 JavaScript 閉包(closure)scope 的優點傳送參數給 callback 函式。看一下這個範例:
function callback(a, b) {
return function() {
console.log('sum = ', (a+b));
}
}
var x = 1, y = 2;
document.getElementById('someelem').addEventListener('click', callback(x, y));
什麼是閉包(closure)?
Closures 指的是一個獨立的變數函式。換句話說,在閉包中定義的函式「記得」它被建立時的環境。在 MDN 文件了解更多。
當 callback 函式被呼叫時,這些方法的參數 x
和 y
都會在 callback 函式的範圍內。
另一個方法你可以使用 bind
。例如:
var alertText = function(text) {
alert(text);
};
document.getElementById('someelem').addEventListener('click', alertText.bind(this, 'hello'));
兩種方法上有細微的性能上的差別,請參考 jsperf。
MEET THE NEW JSTIPS BOOK
You no longer need 10+ years of experience to get your dream job.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.
GET THE BOOK NOW
MEET THE NEW JSTIPS BOOK
The book to ace the JavaScript Interview.
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.
GET THE BOOK NOW