Javascript reference in for-of loop

Viewed 3030

With regular for loop I can do something like that:

for (let i = 0; i < objects.length; i++) {
    delete objects[i];
}

It's not possible with regular for-of loop

for (let o of objects) {
    delete o;
}

That will generate such error

SyntaxError: Deleting local variable in strict mode.

And this is expected, o is a copy. So, is there a way to initialize o as a reference? For example, this is how it's done in C++11:

for (auto &o : objects) {
    // o is a reference to an object from objects
}
3 Answers

You could do that with an Array.prototype.forEach method, which provides the array item along with the index of the item to the iterator function.

var arr = [1, 2, 3];

arr.forEach(function(item, index) {
  delete arr[index];
});
console.log(arr);

delete will never actually delete the underlying object - if a property on an object (or array) is a reference to an object, such as with your objects[i], and you delete the property from the object (which is doable with delete, eg delete objects[i]), then that object (objects[i]) will still exist in memory (for now). It'll only be actually deleted once nothing else can reference it and the garbage collector runs.

For example, with the following code:

const obj = { foo: 'foo' };
const arr = [obj];

Nothing you could to do arr can de-reference the object that obj points to. (You could mutate it, eg for (const prop of arr[0]) delete arr[0][prop], but you can't remove the object itself from memory.)

If each object in the array is not referenced anywhere else, then the way to de-reference all of them so that they'll be GC'd is with your original code:

for (let i = 0; i < objects.length; i++) {
    delete objects[i];
}

or something like it.

JavaScript does not support delete applied to objects - it's a syntax error as posted. The delete operator in JavaScript only ever applies to removing a named property from an object. The type and value of the property is of no consequence.

Hence

let a= [{}];
delete a[0];

removes the property named "0" from a. The anonymous object that was stored in the array has no further references anywhere in code, and will be collected by the GC.

But

 let a = [{}];
 for (let o of a) {
    delete o;
 }

is a syntax error generated by a bad delete expression - there's no property name provided to be deleted.

And this is expected, o is a copy.

may still be alluding to C++. Object data values in JavaScript are references to an object's data structure. It could be a memory pointer but that would be entirely dependent on the JavaScript engine.

The language provides no means of accessing the internal reference/pointer value or performing any address operations on it. A copied object value will refer to the same data structure as the value it was copied from.

Manipulations of an object's properties can be performed on any reference or copied reference to it. Also of note, JavaScript objects do not support destructors. An object's memory resources may be reclaimed by the GC when no accessible code can still access it.

Related