I write JavaScript code and I try to use its functional language nature.
In other functional languages (or even in Ruby), if I don't explicitly set the return value of a function, it will return the value of the last evaluated expression. JavaScript does not follow this pattern. (To be precise, JavaScript always returns a value as well. If nothing was set, then undefined
.)
My question is the following: I have a function that doesn't need to (and does not) return a value. Does it make sense in a functional programming context to have a function with no explicit return value? Or did a fail somewhere if I found myself in this case?
For example, I have the following function. It checks periodically if location hash was changed, and if so, calls the given function.
LIB_hashManager = function(f, context) {
var prev = '';
var pollHash = function() {
if (prev !== window.location.hash) {
prev = window.location.hash;
f.apply(context);
}
};
window.setInterval(pollHash, 100);
};
Should I return here anything?
Update
Meanwhile it came to my mind, that if anytime in the future I'll need to extend the knowledge of LIB_hashManager
, following the functional constructor pattern, I can simply add methods to an object and LIB_hashManager
will return that produced object.
LIB_hashManager = function(f, context) {
// inside logic
// ...
};
And later I may write:
LIB_hashManager = function(f, context) {
// inside logic
// ...
// return public methods
return {
// ...
}
};
So doesn't make sense then to return an empty object in the first case?