7

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

Sap_vr
  • 2,379
  • 2
  • 17
  • 17

4 Answers4

6
String(f1) === String(f2)
Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
jspcal
  • 50,847
  • 7
  • 72
  • 76
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.

Dan Beam
  • 3,632
  • 2
  • 23
  • 27
  • +1 for a more elaborate answer – Andy E Dec 20 '09 at 21:58
  • haha, but it is your answer ... I just don't know how much JavaScript black magic the author has done (like we have) before, :P – Dan Beam Dec 20 '09 at 22:17
  • 7
    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. – kangax Dec 20 '09 at 22:48
  • 1
    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);})();` – Dan Beam Dec 21 '09 at 01:37
  • 1
    Here are few I stumbled upon — http://perfectionkills.com/those-tricky-functions/ , so not something I would rely on. YMMV, of course ;) – kangax Dec 21 '09 at 03:29
  • you're only comparing them to eachother in *the same browser*, so how the functions look **across browsers** is not really that helpful, but I appreciate your time taken to compile that list and will remember these flaws. – Dan Beam Dec 21 '09 at 03:38
  • But Blackberry, for example, always returns *same representation for any 2 different function*. Doesn't that prove this approach unreliable? I also remember cases of Opera mini doing similar thing. This all makes perfect sense in environments like mobile phone browsers, where there's a limited amount of memory available (and so keeping representation of functions is rather wasteful, especially that specs do not put any restrictions on what `Function.prototype.toString` returns ;)) – kangax Dec 21 '09 at 04:13
  • Then, yes, you are right kangax. If all functions look the same when their .toString( ) function is called on BlackBerry, you're right not to use it in your mobile app. Do you have a better solution, or just that it's not really possible? – Dan Beam Dec 21 '09 at 04:40
  • 1
    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. – kangax Dec 21 '09 at 22:39
  • yes, I agree. too bad the author may not, :( – Dan Beam Dec 22 '09 at 06:56
  • @kangax - I lol'ed that Prototype is actually doing this, then (from the source) - https://github.com/sstephenson/prototype/blob/master/src/prototype/lang/function.js#L20 – Dan Beam Apr 27 '11 at 22:26
  • This won't work when comparing 2 same functions which has different scope applied to them via the `bind()` method. this is problematic – vsync Feb 25 '15 at 10:01
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

Toskan
  • 13,911
  • 14
  • 95
  • 185
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);
Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128