简体   繁体   English

将对象的引用推送到数组

[英]Pushing reference of an object to array

As far as i can see in a situation like this: 据我所知,在这种情况下:

var x = [];
var y = {};

y.someProp='asd';

This doesnt work: 这不起作用:

x.push(y);

What I want to do is add a reference of y to x so that later if I will "delete y;" 我想要做的是将y的引用添加到x,以便稍后我将“删除y;” I want it also to be removed from the array x. 我希望它也从数组x中删除。

You are mixing up variables, references and objects. 您正在混合变量,引用和对象。

Doing delete y; delete y; removes the variable y. 删除变量y。 As the variable no longer exists, it will naturally no longer have a value. 由于变量不再存在,它自然不再具有值。 Thus the reference that the variable contained is gone. 因此,包含变量的引用消失了。

Removing the variable will however not in itself remove the object that it was referencing. 但是,删除变量本身不会删除它引用的对象。 The array still contains a reference to the object, and neither of those depend on the existance of the variable. 数组仍然包含对象的引用,这些都不依赖于变量的存在。

There is actually no way of removing the object directly. 实际上没有办法直接删除对象。 You remove objects by destroying all references to it, not the other way around. 您通过销毁对它的所有引用来删除对象,而不是相反。 You want the object to remove it's references, which is simply not how it works. 您希望对象删除它的引用,这根本不是它的工作原理。

That's not how delete works. 这不是delete工作方式。 It will delete the object reference you pass to it - not any other references to the same object. 它将删除传递给它的对象引用 - 而不是对同一对象的任何其他引用。 You're looking for something like a "weak reference" here and I'm not aware of any ways of implementing those in JavaScript. 你在这里寻找类似“弱引用”的东西,我不知道在JavaScript中实现这些的任何方法。

Would this help? 这会有帮助吗?

Var x=[{}]; var y=x[0];

Edit: Sorry, I was a bit brief in my first attempt at answering. 编辑:对不起,我第一次尝试回答时有点简短。 What I'd start asking is: why does y need to "exist" outside of the array in the first place? 我开始问的问​​题是:为什么y首先需要在阵列之外“存在”? Can't you just create an array of objects and use delete or array.splice() to remove the object when you need to? 难道你不能只创建一个对象数组并使用delete或array.splice()在需要时删除对象吗?

Second, if y needs to exist outside of the array, what you need to do is either to create a property that refers to the array index, or a utility function that can scan through the array to find the correct object reference to remove. 其次,如果y需要存在于数组之外,那么您需要做的是创建引用数组索引的属性,或者是可以扫描数组以找到要删除的正确对象引用的实用程序函数。 In other words, something like 换句话说,就像

var x=[]; var y={}; y.arrayIndex=(x.push(y)-1);

// later on, you want to get rid of y

delete x[y.arrayIndex]; delete y;

This doesnt work: 这不起作用:

 x.push(y); x.push(Y); 

Of course it does, it will work exactly as intended - pushing the value of y to the array you created. 当然它确实如此,它将完全按预期工作 - 将y的值推送到您创建的数组。

The delete operator is specifically for deleting properties on objects, if the expression following the operator does not resolve to a property then nothing happens at all. delete操作符专门用于删除对象的属性,如果操作符后面的表达式没有解析为属性,则根本没有任何操作。

var y = {"someprop":true};

delete y;
alert(y.someprop); // alerts "true"

Try it in your browser window, paste the following into your address bar and hit enter: 在浏览器窗口中尝试,将以下内容粘贴到地址栏中,然后按Enter键:

javascript:var y = {"someprop":true}; delete y; alert(y.someprop);

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

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