1

I am working on a little project and one of the objects for the project can include update functions being added to an array that is a property of the object.

Example,

/*
    Add an update function to the layer
    @param function {update} The update to add
*/
Layer.prototype.addUpdate = function (update) {

    // Add the update
    this.updates.push(update);
};

/*
    Remove an update from the layer
    @param function {update} The update to remove
*/
Layer.prototype.removeUpdate = function (update) {

    this.updates.forEach(function (element, index) {

        if (element.toString() === update.toString()) {
            this.updates.splice(index, 1);
        }
    }, this);
};

With the code above I can use it like so;

var layer = new Layer();
var func = function () {
     x = 10;
};
layer.addUpdate(func);
layer.removeUpdate(func);

After reading on the internet about doing it this way to compare the functions equality, everywhere I have read says that it is really bad to do so.

Is using toString() on a function really that bad?

Are there any other ways I can do this while only supplying the function for both parameters when adding and removing an update?

UDPATE

Is there a way to check if 2 variables point to the same reference? Example (pseudo);

var a = 10;
var b = a;
var c = a;

if (b and c point to a) //
GriffLab
  • 2,076
  • 3
  • 20
  • 21

1 Answers1

1

Sure. Compare the functions themselves:

if(element === update) {
    // ...

However, you might have a problem modifying the array while forEach is looping over it.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • this will only work Carl is comparing the exact same function object, not with 2 identical functions. – Ben McCormick Apr 08 '13 at 03:03
  • @ben336: If they aren't the exact same function object, they aren't identical functions. – icktoofay Apr 08 '13 at 03:04
  • @icktoofay: Why not? You can create 2 different functions with the same signature and body. I think that's what ben meant. – mpen Apr 08 '13 at 03:08
  • @Mark: Sure you can create two different functions with the same signature and body, but they can do two completely different things. Case in point: bound functions. – icktoofay Apr 08 '13 at 03:10
  • Is there no way to test if 2 variables point to the same reference, I'll update my question. – GriffLab Apr 08 '13 at 03:11
  • @icktoofay: Binding is essentially like passing a different argument, no? I wouldn't say that's different. – mpen Apr 08 '13 at 03:13
  • 2
    @Mark: Say I shim `bind` (poorly): `Function.prototype.bind = function(thisArg) { var f = this; return function() { return f.apply(thisArg); }; };`. Now I bind two functions: `function a(x) { return x + 1; } function b(x) { return x - 1; } var boundA = a.bind(window); var boundB = b.bind(window);`. Now `boundA` and `boundB` do completely different things, but their signature and body are the same: `function() { return f.apply(thisArg); }`. `boundA.toString() === boundB.toString()`. – icktoofay Apr 08 '13 at 03:16
  • 1
    @CarlG: `===` on functions *does* compare the references. That's what I'm suggesting. – icktoofay Apr 08 '13 at 03:17
  • @icktoofay that is what I was trying to do, I always over complicate things! Thanks for your help. – GriffLab Apr 08 '13 at 03:19
  • @icktoofay: Kind of a weird scenario, but I do see your point. Didn't consider that. Thanks for the enlightenment :-) – mpen Apr 08 '13 at 03:20