Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript : is given function empty?

Tags:

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 }) 
like image 858
Marek Sebera Avatar asked Oct 31 '11 22:10

Marek Sebera


People also ask

Is JavaScript function empty?

isEmpty() returns true if the value is undefined, null, or an empty string.

What does an empty function return in JavaScript?

Definition and Usage The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

How do you define an empty function?

An empty function is basically creating a function without defining any operations inside it. A class named Demo contains an empty function named 'my_empty_fun' which is just completed by placing two flower brackets, without adding any functionality into it.


2 Answers

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.

like image 166
jfriend00 Avatar answered Sep 21 '22 19:09

jfriend00


It seems that you can define a function to retrieve the body of a function(1). I wrote a small (non-definitive) test of this:

http://jsfiddle.net/6qn5P/

Function.prototype.getBody =   function() {     // Get content between first { and last }     var m = this.toString().match(/\{([\s\S]*)\}/m)[1];     // Strip comments     return m.replace(/^\s*\/\/.*$/mg,'');   };  function foo() {     var a = 1, b = "bar";     alert(b + a);     return null; }  console.log(foo.getBody()); console.log(foo.getBody().length); 
like image 26
James Sumners Avatar answered Sep 20 '22 19:09

James Sumners