Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  110] [ 3]  / answers: 1 / hits: 18517  / 13 Years ago, mon, october 31, 2011, 12:00:00

Let's have a function call



function doSomethingAndInvokeCallback(callback){
// do something
callback();
}


I can check if given argument is function if(typeof callback == 'function')



How can I discover, if given callback function is function and isn't empty?



like



doSomethingAndInvokeCallback(function(){
//nothing here
})

More From » javascript

 Answers
1

There is no totally reliable way to know if a function is empty because there are multiple kinds of functions in JS, some implemented with JS and some implemented with native code and you can't know for sure whether the function passed in does anything or not. If you want to limit the passed in function to only very simple JS functions, you could use the mechanisms outlined by other answers here (examining the source of the function). But, I would not recommend doing that in anything but a tightly controlled situation because there are lots of legal javascript ways to break that.



I would suggest that you should change the contract of your function arguments and have the caller pass null or not pass anything (which will make the argument undefined) rather than an empty function. Then, it will be very clear whether they intend to have a function called or not. If they then pass an empty function instead of null or undefined, they are getting the behavior that the interface of the function specifies. The caller can choose the desired behavior and you can implement your function in a more failsafe manner.



Also, one of your main suppositions in your question is not quite right. You cannot safely use typeof x == function to determine if something is a function as that will not work reliably in some older versions of IE for some types of functions. If you want to learn how to detect if something is a function at all, you can learn from jQuery here (even if you're not using it). jQuery has a function it uses internally all the time called jQuery.isFunction() that returns a bool. It uses that mostly for testing arguments to see if a function was passed.



Internally, it calls:



Object.prototype.toString.call(o)


and then examines the result. If the result has Function in it, then test test parameter is a function.



So, using the same technique used in jQuery, you could build your own simple little isFunction routine like this:



function isFunction(test) {
return(Object.prototype.toString.call(test).indexOf(Function) > -1);
}


Of course, if you have jQuery available, you could just use it's own version:



jQuery.isFunction(o)


When there are questions with potential cross browser compatibility issues, I find it instructional to look at how one of the big libraries solves the issue, even if you aren't going to be using that library. You can be sure that the libraries have been vetted against many browsers so a technique they are using is safe. You sometimes have to unwrap all their own internal routines they may use to figure out what they're really doing (which was the case for this function), but you can save yourself a lot of legwork.



You can see a working test bed for this here: http://jsfiddle.net/jfriend00/PKcsM/



In modern browsers typeof fn === function, but in older versions of IE, some functions give a typeof === object which is probably why jQuery uses this other method which does work in those older versions of IE.


[#89354] Saturday, October 29, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billie

Total Points: 101
Total Questions: 114
Total Answers: 98

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
;