7

how to compare two static functions in javascript equal or not equal?

1
  • why do you want to compare them? Commented Nov 1, 2012 at 13:30

4 Answers 4

6
String(f1) === String(f2)
Sign up to request clarification or add additional context in comments.

3 Comments

I think you mean String(f1) === String(f2) unless you have a custom string() method that I'm unaware of :)
Another way of writing it, for the byte-savers out there: ""+f1 === ""+f2
Does it work with (String(f1.bind(this)) === String(f1.bind(this)))? I think not?
6
var f1 = f2 = function( a ){ return a; };

here, you can use f1 === f2 because they're pointing to the same memory and they're the same type

var f1 = function( a ){ return a; },
    f2 = function( a ){ return a; };

here you can use that byte-saver Andy E used (which is implicitly converting the function to it's body's text as a String),

''+f1 == ''+f2.

this is the gist of what is happening behind the scences:

f1.toString( ) == f2.toString( )  

Edit: Looking back on this post over a year after, I agree with @kangax - you should probably never do this.

12 Comments

+1 for a more elaborate answer
Note that var f1 = f2 = function(){ ... } results in f2 becoming a global property, due to undeclared assignment of function to f2. Undeclared assignments are generally harmful, so it's better to avoid that pattern. Also, don't rely on Function.prototype.toString — it's not standardized and varies across browsers.
I just tested the above code in IE 6, 7, 8, FF 2, 3.0, 3.5 (Windows / Linux), Opera 10 (Windows / Linux), Safari 4, and Chrome (Windows / Linux). Which browsers do you develop for exactly? Konqueror, haha? here's the code, someone prove me wrong javascript:(function(){var f1=function(a){return a;},f2=function(a){return a;};alert(f1.toString());alert(f2.toString());alert(""+f1==""+f2);})();
Here are few I stumbled upon — perfectionkills.com/those-tricky-functions , so not something I would rely on. YMMV, of course ;)
I simply try to avoid design which would lead to using function decompilation. You can certainly decompile functions when working in closed environments (e.g. in intranet apps) or for debugging/profiling purposes (during development). Anything that's meant for public web is better left without it, and, as a result, have less chances of blowing up in some less-common environments.
|
0

Whenever I need to compare functions I make sure there is no scope ambiguity and use the same function object.

Say I have some a pair of library functions that take a callback as one of the parameters. For the sake of this example create1minuteCallback function will set a 1 minute timer and call the callback on each tick. kill1minuteCallback will turn off the callback and you must pass the same callback function as you did for create1minuteCallback.

function create1minuteCallback(callback){
//implementation
}

function kill1minuteCallback(callback){
//implementation
}

Quite clearly this will not work as the function we are passing is different on the second line:

create1minuteCallback(function(){alert("1 minute callback is called");});
kill1minuteCallback(function(){alert("1 minute callback is called");});

I normally do this:

function callbackFunc(){alert("1 minute callback is called");}

create1minuteCallback(callbackFunc);
kill1minuteCallback(callbackFunc);

Comments

0

Well, as simply as that - if you are going to compare functions, you do it for a reason I assume. What is your reason? My reason was to not run a certain function twice. I did it this way (just snippet code to get the idea)

   var x = function(){
    console.error("i am a functionX");
  }
   var y = function(){
        console.error("i am a functionX");



  }

    var z = function(){
       console.error("i am a functionZ");



  }
       var x2= x;

   var obj = new Object();
    obj[x] = "";
    obj[x2] = "";
    obj[y] = "";
    obj[z] = "";
    obj.abc = "xaxa";
                for (prop in obj) {
                    if (obj.hasOwnProperty(prop)) {
                      console.error(obj[prop] + " hello " + prop);
                    }
            }

Function x and y are the same, even though they have different whitespaces. x and y are not the same as z, since z has a different console.error. Btw open your firebug console to see it in the jsbin example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.