0

I'm pushing some anonymous functions into an array this way:

myArray.push(function(){ /* some cool code here */});
myArray.push(function(){ /* some cooler code here */});

Then I execute all functions doing

while (myArray.length) { myArray.shift().call();}

The question is: how can I check if a function is already into myArray? I Tried to do a comparison using toSource() but it does not work... Any idea?

Thanks in advance.

3
  • a specific function or any function? Commented Jul 23, 2014 at 12:34
  • @kristjanreinhold that definitely would not work. Commented Jul 23, 2014 at 12:34
  • @Mritunjay :I need to avoid pushing the same function twice into myArray. Commented Jul 23, 2014 at 14:27

3 Answers 3

1

Do your comparison of the functions as a string:

func1.toString() === func2.toString()

This will even work with anonymous functions, but assumes that by equivalent you mean having the same code, character for character.

Sign up to request clarification or add additional context in comments.

3 Comments

I was imagining a situation where you would potentially add the same function in terms of having the exact same code, however you bring up a good point with that example. Two functions given the same input that would produce the same output, would not be shown as equal with this method. I guess you have to define what makes two functions equivalent.
I think that would be overkill and could be near impossible to do in many cases. I imagine this would suit the OP's purpose.
As I said, I'm only pushing anonymous functions into myArray, so your answer seems to suit my needs. Thank you.
1

You can use Array.prototype.indexOf to check whether your array has that function or not:

var foo = function () {};
var bar = function () {};
var arr = [];

var addFunction = function (f) {
    if (arr.indexOf(fn) === -1) {
        arr.push(fn);
    }
};

addFunction(foo);
addFunction(foo);
console.log(arr.indexOf(foo));    // prints 0
console.log(arr.indexOf(bar));    // prints -1

You can also create a map to check whether you added this function or not:

var foo = function foo() {};
var bar = function bar() {};
var arr = [];
var map = {};

var addFunction (fn) {
    if (!map[fn.name]) {
        arr.push(fn);
        map[fn.name] = fn;
    }
};

addFunction(foo);
addFunction(foo);
console.log(arr.indexOf(foo), map['foo']);    // prints 0, function foo() { ... }
console.log(arr.indexOf(bar), map['bar']);    // prints -1, undefined

Note: This second example might not work with anonymous functions.

Comments

0

As you're adding functions to the list, keep a separate object in which you record some sort of identifier that allows you to check whether the function has been added to the array:

  var myArray = [], addedFunctions = {};
  for (var i = 0; i < whatever; ++i) {
     // do whatever
     if (something) {
       if (!addedFunctions[ "cool code 1" ]) {
         myArray.push(function() { /* cool code 1 */ });
         addedFunctions[ "cool code 1" ] = true;
       }
     }

     if (somethingElse) {
       if (!addedFunctions[ "cool code 2" ]) {
         myArray.push(function() { /* cool code 2 */ });
         addedFunctions[ "cool code 2" ] = true;
       }
     }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.