用Javascript破坏'this'? [英] Destroy 'this' with Javascript?

查看:74
本文介绍了用Javascript破坏'this'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在函数中销毁'this',以便从函数中销毁函数的实例。如果纯javascript无法做到这一点。这可能与原型有关吗?这样的事情:

How do I destroy 'this' within a function so that I destroy the instance of a function from within the function. If this is not possible with pure javascript. Is this possible with prototype? So something like this:

this = nil

或类似的东西:

prototype.destroy(this)

我不知道原型或javascript是否有类似内置的东西。是否有一个我可以调用的函数可以轻松地从实例外部销毁实例?

I do not know if prototype or javascript has anything like this built-in. Is there a function I can call that will destroy the instance from outside the instance with ease?

谢谢

推荐答案

你无法在javascript中销毁这个,甚至试图这样做也与javascript中的垃圾收集方式背道而驰。另外,你不能在javascript中分配给这个

You cannot destroy this in javascript and even trying to do so runs counter to how things are garbage collected in javascript. Also, you cannot assign to this in javascript.

你不能手动释放javascript中的东西。相反,你清除javascript中对象的所有引用,当没有其他代码在javascript中引用对象时,那么垃圾收集器才会释放它。

You do NOT manually free things in javascript. Instead, you clear all references to an object in javascript and when NO other code has a reference to an object in javascript, THEN and only then will the garbage collector free it.

由于javascript不允许你分配给这个指针,当你在一个的函数中这个设置为特定对象,您根本无法在该时刻以任何方式释放该对象。您可以确保没有其他对象具有对您的对象的引用,然后,当此方法完成时,如果没有其他任何对该对象的引用,则它将被垃圾收集器释放。

Since javascript does not allow you to assign to the this pointer, when you're in a function that has this set to a particular object, you simply can't cause that object to be freed in any way at that moment. You can make sure that no other objects have a reference to your object and then, when this method finishes, if nothing else has a reference to the object, then it will be freed by the garbage collector.

垃圾收集系统中的内存管理与非垃圾收集语言完全不同。你不要自己解放东西。您清除对事物的引用,以便GC可以在以后清除这些对象,如果没有其他引用它们。

Memory management in a garbage collected system is completely different than in non-garbage collected languages. You don't free things yourself. You clear references to things so that the GC can then clean up those objects at some later time if there are no other references to them.

这是一个示例。假设您的此对象具有存储在全局变量中的属性:

Here's an example. Supposed you have this object with a property stored in a global variable:

// declare global object and add property to it
var myGlobalObject = {};
myGlobalObject.greeting = "Hello";

你没有明确地释放那个全局变量,但如果你想要它指向的对象要被垃圾收集器释放,那么你只需清除对象的引用:

You don't ever free that global variable explicitly, but if you want the object that it points to to be freed by the garbage collector, then you just clear the reference to the object:

myGlobalObject = null;

然后,GC将看到不再有任何代码具有对该对象的引用 myGlobalObject 用于指向并且由于该对象现在无法被任何代码访问,因此它将被GC释放。

Then, the GC will see that there is no longer any code that has a reference to the object that myGlobalObject used to point to and since that object is now unreachable by any code, it will be freed by the GC.

这篇关于用Javascript破坏'this'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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