In the Polyfill Array.prototype.forEachjs mdn section, you can find the following check:
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
In this particular case, why bother with checking the function? During my tests, calling a function directly gives the same behavior
var callback;
callback();
var callback = "";
callback();
var callback = 2;
callback();
var callback = [];
callback();
var callback = {};
callback();
This is actually a little worse with this type of string concatenation, because we can see "[object Object] is not a function":
function newCallback(val){
if (typeof val !== 'function') {
throw new TypeError(val+ ' is not a function');
}
}
newCallback({});
source
share