I have one object:
var callback = {
onValueChange: function () { },
onTabPressed: function () { },
onFocus: function () { }
};
On my page, i have different editors, like textEditor and numericEditor, and i bind them separately:
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);
});
};
My question is:
Is ok to keep callback object outside of the binding functions? Wouldn't each binding function create a copy of the callback object? (which uses extra memory)
Or should i pass the callback object as a parameter to each of the binding functions?