1

I need to detect whether two anonymous functions are the same.

In the console, why does the following line return false?

(function() { alert("hello"); }) === (function() { alert("hello"); })

Is there a comparison operator or other method to identify that these are 'functionally' the same?

EDIT:

People are asking for the use of this.

It's for removing a function that has been previously pushed into an array of functions. See http://jsfiddle.net/w6s7J/ for a simplified test.

Josh Harrison
  • 5,927
  • 1
  • 30
  • 44
  • 2
    `.toString()` at both sides, but it's very crude. – Rob W Jan 24 '14 at 14:15
  • 1
    Why? Does this have any real life use? – Dalorzo Jan 24 '14 at 14:15
  • I'm 99% sure there is a better way to do whatever you are attempting to do – Jeff Shaver Jan 24 '14 at 14:16
  • @Dalorzo not really :) – Mark Walters Jan 24 '14 at 14:16
  • 1
    Would you count these as 'functionally' the same? `(function() { alert("hello"); }) === (function(doNothing) { alert("hello"); })`. I'm afraid there is no guaranteed way to compare. Even something like this `(function() { var a = 'hello'; alert(a); }) === (function() { var b = 'hello'; alert(b); })`. So it all depend on your definition and what you are prepared to check and accept. – Xotic750 Jan 24 '14 at 14:32
  • Comment on your fiddle: Doing it this way is not a good idea. First it's repetitive (you have to write the function body twice!) and second you create the problem of testing for equality. Why not just define the function once outside and then referring to the exact same function? Then you can do `func1 === func2` (http://jsfiddle.net/basilikum/w6s7J/1/). – basilikum Jan 24 '14 at 14:32
  • Thanks for all the advice, I've learned a lot from this. I was trying to do it this way to avoid problems with scope when the callbacks run, but found out about using .call() to set the value of *this* inside callbacks. – Josh Harrison Jan 25 '14 at 21:00
  • Required reading: https://en.wikipedia.org/wiki/First-class_function#Equality_of_functions – Bergi Apr 03 '15 at 15:14

2 Answers2

2

There is no real way to compare 2 different anonymous functions for 'functionality'.

You can check if they are the same object by using your example code.

var func = function () {
    alert('hello');
};

alert(func === func);

The above will work, as you are checking to see that both objects are the same.

The only other method for comparing is to compare them as a string.

var func1 = function () {
    alert("hello");
};

var func2 = function () {
    alert('hello');
};

alert(func1.toString() === func2.toString());

Oops!, they are functionally the same, the difference is the quotes used, So this returns false.

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • Comparing as string is actually pretty safe if the code is not actually typed twice. I had a function where I allowed registering callbacks to some action. I used object comparison to avoid duplication, but the noticed it didn't always work because some of the callbacks were anonymus functions. With string comparison it fixed my issue – LobsterMan May 02 '23 at 14:54
1
(function() { alert("hello"); }) === (function() { alert("hello"); })

Returns false because they are different objects in javascript.

(function() { alert("hello"); }).toString() === (function() { alert("hello"); }).toString()

Returns true because they are the same strings.

Also function objects have a name property which return the name of the function (but it does not work in IE):

var  a = function b() {}
alert(a.name);//write b
infografnet
  • 3,749
  • 1
  • 35
  • 35
Alex
  • 11,115
  • 12
  • 51
  • 64