Why Can't I Save An Object's Methods As Properties Of Another Object Literal
The code below is used to note some methods to run in particular circumstances so they can be called using a simpler syntax. var callbacks = {alter: SPZ.sequenceEditor.saveAndLoadP
Solution 1:
What exactly is the problem? Will the properties be undefined
or the calls just not work correctly?
If the latter, the problem is most likely that when calling the methods, this
will no longer refer to SPZ.sequenceEditor
, but your callbacks
object; to solve this problem, use the helper function bind()
(as defined by several frameworks) or wrap the calls yourself:
var callbacks = {
alter: function() {
returnSPZ.sequenceEditor.saveAndLoadPuzzle.apply(
SPZ.sequenceEditor, arguments);
},
copy: function() {
returnSPZ.sequenceEditor.saveAsCopyAndLoadPuzzle.apply(
SPZ.sequenceEditor, arguments);
},
justSave: function() {
returnSPZ.sequenceEditor.saveAndLoadPuzzle.apply(
SPZ.sequenceEditor, arguments);
}
};
The apply()
is only necessary if the methods take arguments. See details at MDC.
Post a Comment for "Why Can't I Save An Object's Methods As Properties Of Another Object Literal"