javascript - Understanding classes with Mootools -


Sorry for my english, I am french :) I have created an original class called "slider". This class has a "slider_elment" attribute, which is a DIV element. There is also a "destroyer" method in the class, this method destroys the div element.

The slider_alment is considered to be a div that has another DIV named "Delete" CSS class name. When I click on "Remove DIV", then I want to call the "Destructive" method so that DIV disappears.

Here my code is down, and it works graphically as if I want to. My question is: when I destroy the DIV element, then I do not need my slider instance ("MySlender" here). But my code destroys DIV elements, not slider example Does this example still exist? I think yes, so I discovered how could destroy an example of a class with a monotonous but did not get it ... so I think I am doing something wrong, even if my code does this What I graphically illustrate Please help :)

  var slider = new square ({initialize: function (slider_alment) {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 (my element);   

(In fact, this is a simplified code, so that I do not bother you with my whole code)

There is no way to explicitly delete an object in javascript. Best of all, you can do that by removing all the references and hope that your javascript re-implements the implementation memory.

In principle, you can overwrite any reference only (such as with mySlider example) with null . But there can be easily 'inherent' references that you can not control, for example being closed (as used for the events) - you 'throw it out to the garbage collector, but then to make sure you It will be that if any context remains alive and some try to use those properties then nothing bad happens

For elements, Mootools has the destroyed method Which is complete DOM is through subtitles and clears the corresponding reserves of the elements as well as all properties, such events can be extracted from the DOM for the audience.

In your case, @dimeter Christophe has written, if you do not have any external code that calls the slider object, give it to var mySlider There is a need to save a reference in .

And if you do not do that, then only one thing that holds the slider object belongs to the stacked frame alive, which is .bind (this) < / Code> in the addEvent call. When the event is called Fire and Destroy , the event listener is removed, and the JavaScript engine slider is also free to deprecate the object.

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -