onemach onemach - 3 months ago 16
Javascript Question

how do I compare 2 functions in javascript

How do I compare 2 functions in javascript?
I am not talking about internal reference. Say

var a = function(){return 1;};
var b = function(){retrun 1;};


Is it possible to compare
a
and
b
?

Answer
var a = b = function( c ){ return c; };
//here, you can use a === b because they're pointing to the same memory and they're the same type

var a = function( c ){ return c; },
    b = function( c ){ return c; };
//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),

''+a == ''+b.

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

a.toString( ) == b.toString( )  
Comments