使用 Mootools 理解类

发布于 2024-10-27 04:55:04 字数 870 浏览 1 评论 0原文

抱歉我的英语不好,我是法国人:) 我创建了一个名为“Slider”的 Mootools 类。 该类有一个“slider_element”属性,它是一个 DIV 元素。 该类还有一个“destroyer”方法。此方法会破坏 DIV 元素。

slider_element 应该是一个 div,其中包含另一个具有“remove”CSS 类名的 DIV。当我点击“删除DIV”时,我希望调用“destroyer”方法,以便DIV消失。

下面是我的代码,它按照我想要的方式以图形方式工作。 我的问题是:当我销毁 DIV 元素时,我不再需要我的 Slider 实例(此处为“mySlider”)。但我的代码破坏了 DIV 元素,而不是滑块实例。这个实例还存在吗?我想是的。所以我搜索了如何使用 Mootools 销毁类的实例,但没有找到......所以我认为我做错了事情,即使我的代码以图形方式执行了我想要的操作。请帮忙:)

var Slider = new Class({
    initialize: function(slider_element){
        this.slider_element = slider_element;
        this.slider_element.getElements('*[class="remove"]').addEvent('click', this.destroyer.bind(this));
    },
    destroyer: function(){
    this.slider_element.destroy();
    }   
});
var myElement = $('my_slider');
var mySlider = new Slider(myElement);

(实际上,这是一个简化的代码,这样我就不会用我的整个代码打扰您)

Sorry for my english, I'm french :)
I created a Mootools class named "Slider".
This class has a "slider_element" attribute, which is a DIV element.
The class also has a "destroyer" method. This method destroys the DIV element.

The slider_element is supposed to be a div that contains another DIV with a "remove" CSS classname. When I click on the "remove DIV", I want the "destroyer" method to be called, so that the DIV disappears.

Here is my code below, and it works graphically like I want.
My question is : when I destroy the DIV element, I don't need no more my Slider instance (here "mySlider"). But my code destroys the DIV elements, not the slider instance. Do this instance still exist ? I suppose yes. So I searched how to destroy an instance of a class with Mootools, but didn't find ... so I supposed I'm doing something the wrong way, even if my code does what I want graphically. Please help :)

var Slider = new Class({
    initialize: function(slider_element){
        this.slider_element = slider_element;
        this.slider_element.getElements('*[class="remove"]').addEvent('click', this.destroyer.bind(this));
    },
    destroyer: function(){
    this.slider_element.destroy();
    }   
});
var myElement = $('my_slider');
var mySlider = new Slider(myElement);

(in reality, this is a simplified code, so that I don't disturb you with my whole code)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱桃奶球 2024-11-03 04:55:04

JavaScript 中没有办法显式销毁对象。您能做的最好的事情就是删除对它的所有引用,并希望您的 JavaScript 实现重用内存。

因此原则上您可以用 null 覆盖任何引用(例如示例中的 mySlider)。但是很容易存在您无法控制的“隐式”引用,例如在闭包中(用于事件)——您可以通过在之前“清除”引用其他对象的任何属性来帮助垃圾收集器扔掉它,但是如果引用在某个地方存在并且某些东西尝试使用这些属性,那么您必须确保不会发生任何不好的情况。

对于元素,Mootools 具有 destroy 方法,该方法会遍历整个 DOM 子树并清除所有属性以及元素的关联存储(例如事件侦听器),然后再将其从 DOM 中删除。

在您的情况下,正如 @Dimitar Christoff 所写,如果您没有任何调用 Slider 对象上的方法的外部代码,则无需在 var 中保存对其的引用mySlider

如果您不这样做,保持 Slider 对象处于活动状态的唯一方法是来自由 .bind(this)addEvent 调用中。当事件触发并调用 destroy 时,事件侦听器将被删除,并且 JavaScript 引擎也可以自由地释放 Slider 对象。

There is no way to explicitly destroy an object in JavaScript. The best you can do is to remove all references to it and hope that your JavaScript implementation reuses the memory.

So in principle you can just overwrite any references (like mySlider in your example) with null. But there can easily be 'implicit' references that you can't control, in closures for instance (as used for events) -- you might be able to help the garbage collector by 'cleaning out' any properties that reference other objects, before throwing it away, but then you have to make sure nothing bad happens if a reference survives somewhere and something tries to use those properties.

For elements, Mootools has the destroy method that goes through a whole DOM subtree and clears out all properties as well as the associated storage of the elements, such as event listeners, before removing it from the DOM.

In your case, as @Dimitar Christoff wrote, if you don't have any external code that calls methods on the Slider object, you don't need to save a reference to it in var mySlider.

And if you don't do that, the only thing that keeps the Slider object alive is the reference from the stack frame of the closure that is built by .bind(this) in the addEvent call. When the event fires and destroy is called, the event listener is removed, and the JavaScript engine is free to deallocate the Slider object as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文