Function.isFunction

Similar in nature to Array.isArray.
I just found it odd that there is no simple way to do this and there are a bunch of implementations if it.

The basic intent is that:

if (Function.isFunction(x)) {
  x();
}

Would never throw any errors.

Current implementations:


https://api.jquery.com/jQuery.isFunction/ which was deprecated and recommended to use typeof x === "function"

typeof x === 'function' already achieves this, with syntax, so it's impossible for it to break (delete Function.isFunction, for example, would break your suggestion).

The more complex logic you see in lodash, and in https://npmjs.com/is-callable is for older non spec-compliant engines.

2 Likes

And just to add onto that, there's an existing (withdrawn) proposal that looked into rounding out that matrix: http://github.com/jasnell/proposal-istypes

You may want to look into its history, as this commit went from Foo.isFoo(value) to Builtins.is(Foo, value). Also, it was only talked about once in meetings and was withdrawn in favor of Object.prototype.toString as that generally worked for 99% of cases.