[OpenLayers-Users] OpenLayers.Class destroy method question

Eric Lemoine eric.lemoine at camptocamp.com
Fri Nov 11 14:34:25 EST 2011


On Fri, Nov 11, 2011 at 5:48 PM, laurentK2 <lboileau at k2geospatial.com> wrote:
> I'm developping my own classes using the "OpenLayers.Class" class. However,
> I'm wondering something regarding the destroy method.
>
> I created my own class that doesn't inherit from any other. That class has a
> few properties with values and initialize and destroy methods among others.
> In my destroy method, I assign null values to my properties to prevent
> memory leaks. Here's something that looks like it:
>
> ExampleClass = OpenLayers.Class(
> {
>   property: "defaultValue",
>
>   initialize: function(options)
>   {
>     // Do Stuff
>   },
>
>   destroy: function()
>   {
>     // Do Stuff
>     this.property = null;
>   },
>
>   CLASS_NAME: "ExampleClass"
> });
>
> I assumed that calling the destroy method on an object of that class would
> clear that object's properties. However, it seems that the destroy call will
> nullify the assigned properties of the "OpenLayers.Class" object. That means
> that every new instance of the class won't have the defaultly assigned
> property.
>
> Example:
>
> var obj = new ExampleClass(); // obj will have its property to
> "defaultValue"
> obj.destroy(); // Destroy the obj instance.
> var obj2 = new ExampleClass(); // obj will have its property nullified!

I don't think that's correct. Do that in FireBug:

>>> A = OpenLayers.Class({prop: "default", initialize: function() {}, destroy: function() { this.prop = null; }});
>>> a = new A;
>>> a.prop
"default"
>>> a.destroy();
>>> a.prop
null
>>> b = new A;
>>> b.prop
"default"




-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine at camptocamp.com
http://www.camptocamp.com


More information about the Users mailing list