emptyFunction (NodeJS)
Nothing but an empty function closure.
Primarily Intended for optional closures. This allows you to use one empty closure object in your entire project, and whenever you need it, you can just pass it by reference instead of creating a new closure.
install
npm install emptyfunction
usage
Get the closure from the module
var emptyFunction = require('emptyfunction');
Whenever there's an optional closure, just add the empty function if no closure is provided
function MyClass(closureArgument) {
if (typeof closureArgument === "function") {
this.someFunction = closureArgument
} else {
this.someFunction = emptyFunction;
}
}
Write your class as if the function exists
MyClass.prototype.doSomething = function() {
this.someFunction();
}
identifying
Sometimes you might need to verify if the closure is an empty function.
If you're inside the same package you can use:
this.someFunction === emptyFunction
If not, a more hacky solution is to use:
this.someFunction.name === 'emptyFunction'