【发布时间】:2013-04-08 02:57:02
【问题描述】:
我正在处理一个小项目,该项目的其中一个对象可以包括更新函数,该函数被添加到作为对象属性的数组中。
例子,
/*
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);
};
通过上面的代码,我可以像这样使用它;
var layer = new Layer();
var func = function () {
x = 10;
};
layer.addUpdate(func);
layer.removeUpdate(func);
在网上阅读了关于这样做比较函数相等性的信息后,我读到的所有地方都说这样做真的很糟糕。
在函数上使用toString() 真的那么糟糕吗?
在添加和删除更新时只为两个参数提供函数的同时,我还有其他方法可以做到这一点吗?
UDPATE
有没有办法检查两个变量是否指向同一个引用?示例(伪);
var a = 10;
var b = a;
var c = a;
if (b and c point to a) //
【问题讨论】:
-
toString在存在不同 cmet 的情况下将不起作用
标签: javascript function tostring equality