zdefiniowałem dwie funkcje tablicy:Jak wywołać zewnętrzne "to" w funkcji wewnętrznej?
Array.prototype.remove = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
this.removeAt(i);
}
}
};
Array.prototype.removeAll = function(array2) {
array2.forEach(function(item) {
this.remove(item); // remove not found!!
});
}
Ale w funkcji removeAll
, donosi, że function remove is not found
. Naprawię to tak:
Array.prototype.removeAll = function(array2) {
var outer = this;
array2.forEach(function(item) {
outer.remove(item);
});
}
Ale to jest brzydkie. Czy istnieje lepszy sposób?
Nie sądzę, że jest to niesamowicie brzydkie i działa. "To" w twoim foreach zostało zmienione na pozycję w iteracji, więc nie widzę zbyt wiele możliwości. – Paddy