如何正确取消引用然后删除JavaScript对象? [英] How to correctly dereference then delete a JavaScript Object?

查看:87
本文介绍了如何正确取消引用然后删除JavaScript对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道从内存中完全取消引用JavaScript对象的正确方法.为了确保将其删除而不将其悬空在内存中,并确保垃圾回收器删除了该对象.

I would like to know the correct way to completely dereference a JavaScript Object from memory. To ensure it's deletion without it dangling in memory, and that the garbage collector removes the object.

当我看着这个问题时,删除JavaScript中的对象.据解释,如果删除对象的所有引用,GC将从内存中将其删除.我想知道如何从具有方法和属性的对象中删除引用.

When I looked and this question Deleting Objects in JavaScript. It was explained that if you delete all the references of object, the GC will remove it from memory. I want to know how do I go about removing references from an object that has both methods and properties.

假设您具有通过 function 创建的对象,并且该对象同时具有方法和属性.说它看起来像这样:

Suppose you have and object that was created by using function, and the object has both methods and properties. Say it looks something like this:

function myObject(x, y) {
   this.x = x; 
   this.y = y;

   this.myMethod = function() {
      // method code
   }
}

var myInstance = new myObject(24, 42) // How to remove this completely?

注意:这是一个示例,我了解您可以将 prototype 用于方法.

Note: This is an example, I understand you can use prototype for methods.

我知道我不能只说 delete myInstance .因此,在这种情况下,要完全删除对象,我需要在其所有属性上调用 delete ,然后在实例上调用 delete ,像这样吗?

I know I can't just say delete myInstance. So in this case to completely delete the object will I need to call delete on all it's properties, and then call delete on the instance, like so?

delete myInstance.x;
delete myInstance.y;
delete myInstance; // I'm not sure if this is necessary.

这项工作吗?还是我还需要删除它的方法(如果这样的话,怎么做)?

Would this work? Or do I need to also delete it's methods (and if so how)?

或者也许有更好,更简单的方法来做到这一点?

Or perhaps there is a better and simpler way to do this?

推荐答案

JavaScript是一种垃圾收集语言.仅当没有其他代码引用该对象时,它才会清除该对象.这些其他引用需要超出范围(而不是由闭包保存),或者您可以将其他变量设置为其他变量,以使它们不会指向您的对象.当没有其他引用对象的变量时,垃圾收集器将自动对其进行处理,包括其具有的所有属性(假定这些属性本身都不是某些对象所引用的对象,但是即使如此,主机对象将被清除,只有属性中的对象将继续存在.)

Javascript is a garbage collected language. It will clean up an object ONLY when there is no other code that has a reference to it. Those other references need to either go out of scope (and not be held by a closure) or you can set those other variables to something else so they don't point at your object. When there are no other variables with a reference to your object, it will be automatically taken care of by the garbage collector, including any properties it has (assuming none of those properties are themselves objects that something has a reference to - but even then the host object would be cleaned up and only the object in the property would continue to live on).

您无法使用Java脚本以其他任何方式删除对象.

You cannot delete an object any other way in Javascript.

因此,要删除由此创建的对象:

So, to remove the object created by this:

var myInstance = new myObject(24, 42) // How to remove this completely?

只需清除 myInstance 就像这样:

myInstance = null;

您完全不需要从 myInstance 中手动删除属性.如果没有人引用母对象,并且所有属性都不是某人引用的对象,则垃圾收集器将为您清除所有内容.

You don't need to manually delete the properties from myInstance at all. If nobody has a reference to the mother object and none of the properties are objects that someone has a reference to, then the garbage collector will just clean everything up for you.

delete 运算符主要用于在您希望保留母对象时(例如,当您只想删除属性时)从对象中删除属性.

The delete operator is primarily for removing properties from an object when you want the mother object to remain (e.g. when you just want to remove a property).

这篇关于如何正确取消引用然后删除JavaScript对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆