This code from ryan niemeyer on this blog bost declares an empty function(named result) and then adds properties to the function:
ko.dirtyFlag = function(root, isInitiallyDirty) {
var result = function() {},
_initialState = ko.observable(ko.toJSON(root)),
_isInitiallyDirty = ko.observable(isInitiallyDirty);
result.isDirty = ko.computed(function() {
return _isInitiallyDirty() || _initialState() !== ko.toJSON(root);
});
result.reset = function() {
_initialState(ko.toJSON(root));
_isInitiallyDirty(false);
};
return result;
};
What advantage does this serve over simply creating an object and assigning the same properties before returning the object?
edit
in response to the comment requesting how i would expect it to look: either declaring
var result={};
in the declarations, or as a style thing:
ko.dirtyFlag = function(root, isInitiallyDirty) {
var _initialState = ko.observable(ko.toJSON(root)),
_isInitiallyDirty = ko.observable(isInitiallyDirty);
return {
isDirty : ko.computed(function() {
return _isInitiallyDirty() || _initialState() !== ko.toJSON(root);
}),
reset : function() {
_initialState(ko.toJSON(root));
_isInitiallyDirty(false);
}
};
};
but the semantics are irrelevant - what does a shell of a function returned provide to the consuming code/developer calling the function?
objector why they are declaring afunctionin a certain way? If the latter, can you provide an example of how you expected the code to look?result()) without triggering a runtime error.