销毁 javascript (jquery) 中的函数
有谁知道如何销毁 javascript (jquery) 函数吗? 我正在使用 jquery“可选择”,并且在可选择的“停止”事件上触发函数调用“编辑”。
在这个“编辑”功能中,我嵌套了带有大量“点击”事件的开关功能 我在每个“点击”事件中都有很多功能。我的问题是, 每次我触发函数“编辑”内的“可选”函数和事件时,都会再次触发,但以前的函数和事件仍然存在。 我现在要做的就是在可选择的“开始”上取消绑定“编辑”功能中的每个事件。
这是内存泄漏问题吗? 有没有办法“销毁”javascript中的函数? 我试图在函数结束时将函数声明为 null,但这不起作用。它里面的函数和事件仍然存在。
有人知道吗?
演示页面在这里 --> http://dreamerscorp.com/test/test01/javascript_destory_test.html
编辑2009/ 10/31 :) 非常感谢您的帮助,您的评论对我非常有用,再次感谢!
Is there anyone who knows how to destroy a javascript (jquery) function?
I'm using jquery "selectable" and a function call "edit" is fired on selectable "stop" event.
Inside this "edit" function I have nested switch functions with a lot of "click" events
and I have many functions within each "click" event. My problem is,
every time I fire the "selectable" functions and events inside the function "edit" is fired again but the previous functions and events still exist.
What i do now is to unbind every event in the function "edit" on selectable "start" even.
Is this a memory leak problem?
and is there a way to "destroy" functions in javascript?
i have tried to declare the function to null when the function ends but this does not work. functions and events inside it still exist.
anyone have a clue?
demo page here -->
http://dreamerscorp.com/test/test01/javascript_destory_test.html
edit 2009/10/31
:) thanks a lot for your helps, your comments are very useful to me, thanks again!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试使该函数无效,或者覆盖它并分配一个不执行任何操作的匿名函数:
您也可以在函数本身内执行此操作:
You can try to nullify the function, or override it assigning an anonymous function that does nothing:
You can also do it within the function itself:
要在 jquery 中取消绑定事件,请使用 unbind 函数 http://docs.jquery.com/Events/unbind
在此示例中,每次单击“something”时,都会向“anotherOne”添加一个事件处理程序,因此如果单击三次,您将获得三个事件处理程序。
在这里,您保证在“anotherOne”上只有一个点击处理程序。
无需显式销毁以前的处理程序。
to unbind events in jquery use unbind function http://docs.jquery.com/Events/unbind
in this example, every time "something" is clicked, an event handler is added to "anotherOne", so if you click three times, you'll get three event handlers.
here you're guaranteed to have only one click handler on "anotherOne".
There's no need to destroy previous handler explicitly.
基本上,您需要删除对这些函数的所有引用,以便 JavaScript 垃圾收集器可以收集它们。如果已绑定,则需要解除绑定。如果其中有其他变量指向它们,则需要将它们设置为 null。
如果您发布一些代码可能会有所帮助;那么我们就可以给出更好的答案。
...编辑:
这里发生的是,您正在创建一个比包含函数更长寿的闭包:
换句话说,在函数内部,您说,“将此函数附加到 DOM对象”,并且您正在内联执行此操作。 JavaScript 从外部上下文捕获变量并在对内部上下文的引用处于活动状态时使它们保持活动状态。
您需要做的是在非内联的地方定义函数,然后使用它们:
这还演示了如何使用 jQuery 单击处理程序中的 data 属性在附加处理程序和使用它的时间之间存储数据(而不是将数据存储在一个闭包中,该闭包会将作用域链保留在内存中)。当您只是在那里使用内联定义的函数时(例如,$.each 的主体),使用内联定义的函数是没问题的,但当您附加事件处理程序时,就不行了。
Basically you need to remove all references to those functions so that the JavaScript garbage collector can collect them. If they are bound, you need to unbind them. If there are other variables in there that point to them, they need to be set to null.
It might help if you posted some code; then we can give a better answer.
...EDIT:
What's happening here is, you're creating a closure that will outlive the containing function:
In other words, inside a function, you're saying, "Attach this function to a DOM object," and you're doing it inline. JavaScript captures variables from outer contexts and keeps them alive while the reference to the inner context is alive.
What you need to do is to define the functions somewhere not inline and then use them:
This also demonstrates how to use the data property in jQuery click handlers to store data in between the time you attach the handler and the time you use it (instead of storing that data in a closure that will keep the scope chain in memory). Using inline-defined functions is fine when you're just using it right there (like the body of a
$.each
, for instance) but it's not OK when you're attaching event handlers.要“销毁” javascript 中的函数,只需确保该函数变得不可访问即可。这将使该函数有资格进行回收。 需要注意的一件事是,JavaScript 变量是基于作用域(而不是单个变量)绑定的,如果将绑定保留到作用域,则包含许多未使用对象的作用域可能会持续存在: 要提供更多帮助,需要了解特定代码及其使用方式。
请参阅 javascript 删除运算符作为删除变量或对象成员的一种方法。将值设置为 null/undefined/other-object 会删除到达先前引用的对象的方法(尽管否则可能仍然可以到达,因此不会被回收),但不会删除变量/成员。
To "destroy" a function in javascript, simply ensure that the function becomes unreachable. This will enable the function to be eligible for reclamation. One thing to watch out for is that javascript variables are bound based on scopes (not as individual variables) and a scope, with many unused objects, may persist if a binding is kept to the scope: to provide any more help requires knowledge of the specific code and how it is used.
Please see the javascript delete operator as one way of removing a variable or object member. Setting the value to null/undefined/other-object removes on method of reaching the object previously referenced (although it might still be reachable otherwise and thus not be reclaimed) but does not get rid of the variable/member.