在下面的代码中,
body.on(event, bindSelector, callback);
我将回调作为对此函数(kit.callback.renderPage)
的引用传递。
我希望(kit.callback.renderPage)
也接受参数。如何从下面的init()
函数传递一个对象作为参数?
jQuery(document).ready(function($) {
(function(send, config) {
var kit = {
selectors: {
"body": "body",
"start": "[data-ckit-toggle-on]"
},
callback: {
renderPage : function() {
console.debug('opt');
}
},
getSelectors: function(selector) {
return $(selector);
},
init: function() {
kit.bindNow(
kit.events.chatToolbar,
kit.selectors.body,
kit.selectors.start,
kit.callback.renderPage
);
},
bindNow: function(event, bindTo, bindSelector, callback) {
var body = kit.getSelectors(bindTo);
body.on(event, bindSelector, callback);
}
};
//starting point
kit.init();
})(interface, settings);
});
答案 0 :(得分:3)
您可以将回调包装在内联/匿名函数中,例如:
bindNow: function(event, bindTo, bindSelector, callback) {
var body = kit.getSelectors(bindTo);
body.on(event, bindSelector, function() {
callback.call(this, param1, param2);
});
}
或
kit.bindNow(
kit.events.chatToolbar,
kit.selectors.body,
kit.selectors.start,
function() {
kit.callback.renderPage.call(this, param1, param2);
}
);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call