简体   繁体   English

for-of循环中的Javascript参考

[英]Javascript reference in for-of loop

With regular for loop I can do something like that: 使用常规的for循环,我可以执行以下操作:

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

It's not possible with regular for-of loop 使用常规for-of循环是不可能的

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

That will generate such error 那会产生这样的错误

SyntaxError: Deleting local variable in strict mode. SyntaxError:在严格模式下删除局部变量。

And this is expected, o is a copy. 这是预料之中的, o是副本。 So, is there a way to initialize o as a reference? 那么,有没有一种方法可以将o初始化为引用? For example, this is how it's done in C++11: 例如,这是在C ++ 11中完成的过程:

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

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. 您可以使用Array.prototype.forEach方法来实现,该方法将数组项以及该项的索引提供给迭代器函数。

 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). delete不会真正删除基础对象-如果对象(或数组)上的属性是对对象的引用(例如您的objects[i] ,而您从对象中删除了该属性(可以使用delete ,例如, delete objects[i] ),则该对象( objects[i] )仍将存在于内存中(目前)。 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. 没事你可以做arr可以去参考的对象obj点。 (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.) (您可以对其进行突变 ,例如for (const prop of arr[0]) delete arr[0][prop] ,但是您无法从内存中删除对象本身。)

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: 如果数组中的每个对象均未在其他任何地方引用,则取消引用所有对象以便将它们进行GC处理的方法是使用原始代码:

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. JavaScript不支持delete应用于对象-这是发布时的语法错误。 The delete operator in JavaScript only ever applies to removing a named property from an object. JavaScript中的delete运算符仅适用于从对象中删除命名属性。 The type and value of the property is of no consequence. 属性的类型和值无关紧要。

Hence 于是

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

removes the property named "0" from a . 去除名为“0”的性质a The anonymous object that was stored in the array has no further references anywhere in code, and will be collected by the GC. 存储在数组中的匿名对象在代码中的任何地方都没有其他引用,并且将由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. 是由错误的delete表达式生成的语法错误 -没有提供要删除的属性名称。

And this is expected, o is a copy. 这是预料之中的,o是副本。

may still be alluding to C++. 可能仍然暗示着C ++。 Object data values in JavaScript are references to an object's data structure. JavaScript中的对象数据值是对对象数据结构的引用。 It could be a memory pointer but that would be entirely dependent on the JavaScript engine. 它可以是一个内存指针,但是完全取决于JavaScript引擎。

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. 还要注意的是,JavaScript对象不支持析构函数。 An object's memory resources may be reclaimed by the GC when no accessible code can still access it. 当无法访问的代码仍无法访问对象时,GC可能会回收对象的内存资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM