問題:如何訪問shortcut
或action
或其他局部變量的javascript:通過局部變量的匿名函數
相關:類似的問題,但沒有成功:
- Passing local variable to loader anonymous handler function
- how to pass local variables when assigning mouseover via anonymous function?
- javascript anonymous function parameter passing
- How to pass variable to anonymous function
Java解決方案: 設置final
改性劑,在匿名函數需要
目標源代碼變量:
//plugin.buttons is collection of button objects
for (var i in plugin.buttons) {
var button = plugin.buttons[i];
var icon = button.icon;
var text = button.text;
var shortcut = button.shortcut;
var action = button.action; //action is a function (or method)
if (shortcut != null && shortcut.length > 0) {
if ($.isFunction(action)) {
console.log(shortcut); //it's valid shortcut
//using jQuery hotkey plugin
$('div[contenteditable]').bind('keydown', shortcut, function() {
console.log(shortcut); //it's undefined
action.call(); //it's undefined also
return false;
});
}
}
}
如圖所示,因爲JS可以通過關閉...編輯到達那些瓦爾它應該只是工作:哎呦,在功能包住for循環,這是書中缺乏循環範圍的最古老的主要解決方法。 – dandavis