Pratik
Pratik

Reputation: 11

Javascript comparing if 2 functions are exact same

I'm stuck with this problem. Need to check if the 2 functions are the same or refer to the same. So the scenario is kind of like this : fn1 function are anonymous.

function fnName(args) {
if(this.fn1 === this.json1.fn1)
//this is true
else
//this is false
}

here both this.fn1 and this.json1.fn1 points to the same function definition. Is there a way to find out if they are pointing the same or not ?

I have tried using function.toString() but this gives the same output for any function i.e;

function() { [native code] }

and on this being compared it gives true as the output for any 2 function that are not even same.

On comparing === It's not considering them as same. On debugging it shows that it is pointing to the function at the same line.

On doing Object.is(this.fn1,this.json1.fn1); is returning false , which means they are not the same object.

How these functions are set to the attribute are through using the bind function such as :

fn1 = fn1.bind(this);
json1["fn1"] = fn1.bind(this)

So now we know these are 2 different Objects

Upvotes: 1

Views: 1959

Answers (2)

Scott Marcus
Scott Marcus

Reputation: 65806

Functions are objects in JavaScript. Even two functions that are written exactly the same are still two distinct objects in memory and will never be equal.

All you can do is convert the functions to strings and compare the strings. I would guess though that you didn't actually invoke the .toString() function during your comparison expression and instead compared the actual .toString function code.

var o1 = {
  foo: function (message){
    console.log(message);
  }
};

var o2 = {
  log: function (message){
    console.log(message);
  }
};

var o3 = {
  log: function (msg){
    console.log(msg);
  }
};

var test = o1.foo;


function compare(f1, f2){
  // You must convert the functions to strings and compare those:
  console.log(f1.toString() === f2.toString());
}

compare(o1.foo, o2.log);  // true - the two functions are identical
compare(o1.foo, o3.log);  // false - the two functions are not identical
compare(o1.foo, test);    // true - the two variables reference the same one function

// This is just to show how not properly calling toString() affects the results:
console.log(o1.foo.toString);   // function toString() { [native code] }
console.log(o1.foo.toString()); // function (message){ console.log(message); }

Upvotes: 1

FisNaN
FisNaN

Reputation: 2865

Consider the example below:

var fun1 = function() { console.log('fun1') };
var fun2 = fun1;
var fun3 = fun1.bind({});

console.log(fun1 === fun2); // true
console.log(fun1 === fun3); // false

function test() {
  fun1 = () => { console.log('new function') }
  fun1();
  fun2();
  fun3();
  console.log(fun1 === fun2); // false
}

fun1();
fun2();
fun3();
test();

  • fun3 is a copy of fun1, it returns false in comparision.
  • fun2 and fun1 are the reference to the same function.
  • Inside, test() function, assign fun1 to a new function. However, fun2 is still pointing to the old function, so it returns false when comparing them.

So, it is safe to compare 2 references of functions using ===.

Upvotes: 0

Related Questions