javascript tutorial - [Solved-5 Solutions] How can we check if a javascript variable is function type ?
- javascript - java script - javascript array
Problem:
Suppose we have any variable, which is defined as follows:
We want a function which checks if the type of the variable is function-like. i.e. :
How can we check if the variable 'a' is of type function in the way defined above?
Solution 1:
Sure underscore's way is more efficient, but the best way to check, when efficiency isn't an issue, is written on underscore's page linked by @Paul Rosania.
Inspired by underscore, the final isFunction function is as follows:
Solution 2:
Solution 3:
@grandecomplex: There's a fair amount of verbosity to our solution. It would be much clearer if written like this:
Solution 4:
If we use jquery we can use the isFunction:
Solution 5:
Try instanceof: It seems that all functions inherit from the "Function" class: