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) //