Currently, I am working with a single object:
var callback = {
onValueChange: function () { },
onTabPressed: function () { },
onFocus: function () { }
};
In my webpage, I have various editors such as textEditor
and numericEditor
, and I bind them individually:
function bindEditors() {
var editors = $(".editor");
editors.each(function (i) {
var editor = $(this);
var editorType = editor.attr("data-editorType");
if (editorType == "textEditor") {
bindTextEditor(editor);
} else if (editorType == "numericEditor") {
bindNumericEditor();
}
});
};
function bindTextEditor(editor) {
editor.bind("change", function () {
// calculate value
callback.onValueChange($(this), value);
});
};
function bindNumericEditor(editor) {
editor.bind("change", function () {
// calculate value
callback.onValueChange($(this), value);
});
};
The main concern I have is:
Is it appropriate to maintain the callback
object outside of the binding functions? Will each binding function create its own copy of the callback
object, leading to unnecessary memory usage?
Alternatively, should I pass the callback
object as a parameter to each of the binding functions?