【发布时间】:2014-07-30 20:35:54
【问题描述】:
我有一个数组来跟踪这样的 javascript 对象:
var myArray = [];
对象看起来像这样:
Main.prototype.myObject = function (x, y) {
this.x = x;
this.y = y;
this.moveObject = function () {
// Change x, y coordinates
if (someEvent) {
// Delete myself
// deleteObject();
}
};
this.deleteObject = function () {
// Delete code
}
};
对象像这样被推入数组中:
myArray.push(new main.myObject(this.x, this.y));
现在,有没有一种方法可以在不知道 myArray 中的索引的情况下使用 this 删除对象的特定实例?
我宁愿保持 for 循环干净,并在已经存在的 moveObject() 函数中进行删除。
【问题讨论】:
-
您不能从对象内部删除对象。你需要在其他地方循环遍历数组,找到你想要的对象,然后拼接对象什么的。
标签: javascript arrays oop