[OpenLayers-Dev] 2.10 and 2.11-RC1 OpenLayers.Class behavior changes

RICHARD Didier didier.richard at ign.fr
Sun Jul 24 17:42:21 EDT 2011


> Sorry, missed to add the last line. So the working example would be:
>
> var X= OpenLayers.Class({
>    initialize:function() { this._p= "A.initialize";
> OpenLayers.Console.info(this._p); },
>    pa:"PA",
>    p_:null,
>    ma:function() { OpenLayers.Console.info("A.ma="+this.pa); }
> });
> xProto = X.prototype;
> xProto.initialize = function() { this.p_= "A.initialize-new";
> OpenLayers.Console.info(this.p_); }
> X = xProto.initialize;
> X.prototype = xProto;
>

Great, it's now working (sorry for having replied too fastly to your
previous email).

I will try to write a method that propagate such overload to a full
classes tree. My tests look like ;

    A= OpenLayers.Class({
        initialize:function() { this._p= "A.initialize";
OpenLayers.Console.info(this._p); },
        pa:"PA",
        p_:null,
        ma:function() { OpenLayers.Console.info("A.ma="+this.pa); }
    });
    A.B= OpenLayers.Class(A,{
        pa:"PA-bis",
        pb:"PB",
        mb:function() { OpenLayers.Console.info("B.mb="+this.pa+"
"+this.pb); }
    });
    A.B.D= OpenLayers.Class(A.B,{
        initialize:function() { this._p= "D.initialize";
OpenLayers.Console.info(this._p); }
    });
    A.B.E= OpenLayers.Class(A.B,{
        initialize:function() { this._p= "E.initialize";
OpenLayers.Console.info(this._p); }
    });
    A.C= OpenLayers.Class(A,{
        initialize:function() { this._p= "C.initialize";
OpenLayers.Console.info(this._p); },
        pc:"PC",
        ma:function() { OpenLayers.Console.info("C.ma="+this.pa+"
"+this.pc); }
    });
    A.C.F= OpenLayers.Class(A.C,{
        pf:"PF",
        mf:function() { OpenLayers.Console.info("F.mf="+this.pa+"
"+this.pc+" "+this.pf); }
    });
    A.C.G= OpenLayers.Class(A.C,{
        pg:"PG",
        ma:function() { OpenLayers.Console.info("G.ma="+this.pa+"
"+this.pc+" "+this.pg); }
    });

    OpenLayers.overload= function(P,F) {
        ....
    });

    A= OpenLayers.overload(A, {
        initialize:function() { this.p_= "A.initialize-new";
OpenLayers.Console.info(this.p_); },
        pa:"PA-new"
    });

    OpenLayers.Console.log("==============================================
overloading A:");
    OpenLayers.Console.log("A:");
    try {
        var a2= new A();
        OpenLayers.Console.assert((a2 instanceof A));
        OpenLayers.Console.assert((a2.p_=="A.initialize-new"));
        a2.ma();
    } catch(exa) {
        OpenLayers.Console.warn(exa);
    }

    OpenLayers.Console.log("A.B:");
    try {
        var b2= new A.B();
        OpenLayers.Console.assert((b2 instanceof A));
        OpenLayers.Console.assert((b2 instanceof A.B));
        OpenLayers.Console.assert((b2.p_=="A.initialize-new"));
        b2.ma();
        b2.mb();
    } catch(exb) {
        OpenLayers.Console.warn(exb);
    }

    OpenLayers.Console.log("A.B.D:");
    try {
        var d2= new A.B.D();
        OpenLayers.Console.assert((d2 instanceof A));
        OpenLayers.Console.assert((d2 instanceof A.B));
        OpenLayers.Console.assert((d2 instanceof A.B.D));
        OpenLayers.Console.assert((d2.p_=="D.initialize"));
        d2.ma();
        d2.mb();
    } catch(exd) {
        OpenLayers.Console.warn(exd);
    }

    OpenLayers.Console.log("A.B.E:");
    try {
        var e2= new A.B.E();
        OpenLayers.Console.assert((e2 instanceof A));
        OpenLayers.Console.assert((e2 instanceof A.B));
        OpenLayers.Console.assert((e2 instanceof A.B.E));
        OpenLayers.Console.assert((e2.p_=="E.initialize"));
        e2.ma();
        e2.mb();
    } catch(exe) {
        OpenLayers.Console.warn(exe);
    }

    OpenLayers.Console.log("A.C:");
    try {
        var c2= new A.C();
        OpenLayers.Console.assert((c2 instanceof A));
        OpenLayers.Console.assert((c2 instanceof A.C));
        OpenLayers.Console.assert((c2.p_=="C.initialize"));
        c2.ma();
    } catch(exc) {
        OpenLayers.Console.warn(exc);
    }

    OpenLayers.Console.log("A.C.F:");
    try {
        var f2= new A.C.F();
        OpenLayers.Console.assert((f2 instanceof A));
        OpenLayers.Console.assert((f2 instanceof A.C));
        OpenLayers.Console.assert((f2 instanceof A.C.F));
        OpenLayers.Console.assert((f2.p_=="C.initialize"));
        f2.ma();
        f2.mf();
    } catch(exf) {
        OpenLayers.Console.warn(exf);
    }

    OpenLayers.Console.log("A.C.G:");
    try {
        var g2= new A.C.G();
        OpenLayers.Console.assert((g2 instanceof A));
        OpenLayers.Console.assert((g2 instanceof A.C));
        OpenLayers.Console.assert((g2 instanceof A.C.G));
        OpenLayers.Console.assert((g2.p_=="C.initialize"));
        g2.ma();
    } catch(exg) {
        OpenLayers.Console.warn(exg);
    }

    OpenLayers.Console.log("done!");

The method has to work in both 2.10 and 2.11 in the end ...

regards,

didier

> Andreas.
>
> On Jul 24, 2011, at 23:17 , Andreas Hocevar wrote:
>
>> Hi Richard,
>>
>> you are right of course, my bad. You would have to re-assign the
>> prototype of the old class, which gets lost when you re-assign X.
>> Something like
>>
>> var X= OpenLayers.Class({
>>   initialize:function() { this._p= "A.initialize";
>> OpenLayers.Console.info(this._p); },
>>   pa:"PA",
>>   p_:null,
>>   ma:function() { OpenLayers.Console.info("A.ma="+this.pa); }
>> });
>> xProto = X.prototype;
>> xProto.initialize = function() { this.p_= "A.initialize-new";
>> OpenLayers.Console.info(this.p_); }
>> X = xProto.initialize;
>>
>> Andreas.
>>
>> On Jul 24, 2011, at 23:05 , RICHARD Didier wrote:
>>
>>>
>>>> Hey Schuyler,
>>>>
>>>> I'm not concerned about breaking backwards compatibility here. We
>>>> never
>>>> encouraged people to override the initialize method by re-assigning it
>>>> on
>>>> the prototype, and we always advertised the initialize method as
>>>> constructor. With 2.11, finally, the initialize method is a real
>>>> constructor, i.e.
>>>>
>>>> X.prototype.initialize === X.
>>>>
>>>> So instead of
>>>>
>>>> X.prototype.initialize = function() { ... }
>>>>
>>>> which we never encouraged people to do anyway, people could now do
>>>>
>>>> X = X.prototype.initialize = function() { ... }
>>>>
>>>
>>> Thanks for remining this, but what about the new paradigm. Doing :
>>>
>>> X= OpenLayers.Class({
>>>   initialize:function() { this._p= "A.initialize";
>>> OpenLayers.Console.info(this._p); },
>>>   pa:"PA",
>>>   p_:null,
>>>   ma:function() { OpenLayers.Console.info("A.ma="+this.pa); }
>>> });
>>> X = X.prototype.initialize = function() { this.p_= "A.initialize-new";
>>> OpenLayers.Console.info(this.p_); }
>>> try {
>>>   var x1= new X();
>>>   OpenLayers.Console.assert((x1 instanceof X));
>>>   OpenLayers.Console.assert((x1.p_=="A.initialize-new"));
>>>   x1.ma();
>>> } catch(ex) {
>>>   OpenLayers.Console.warn(ex);
>>> }
>>>
>>> gives :
>>> TypeError: x1.ma is not a function
>>>
>>> It does not although work with 2.10 (same exception).
>>> Did I miss something ?
>>>
>>> didier
>>>
>>>> Andreas.
>>>>
>>>> On Jul 24, 2011, at 22:07 , Schuyler Erle wrote:
>>>>
>>>>>
>>>>> On Jul 24, 2011, at 4:43 AM, RICHARD Didier wrote:
>>>>>
>>>>>> With 2.10, overloading constructors was as simple as overwriting the
>>>>>> initialize prototype :
>>>>>>
>>>>>> X.prototype.initialize= function () { ...}
>>>>>>
>>>>>> In 2.11-RC1, it breaks as the new prototype is not the constructor !
>>>>>> ...
>>>>>>
>>>>>> BTW, as this new OpenLayers.Class is a major change in OpenLayers,
>>>>>> don't
>>>>>> you thing it is more a 3.0 feature than a 2.x ?
>>>>>
>>>>> I confess I'm a little concerned about this... Are we breaking
>>>>> backwards
>>>>> compatibility with such a change?
>>>>>
>>>>> SDE_______________________________________________
>>>>> Dev mailing list
>>>>> Dev at lists.osgeo.org
>>>>> http://lists.osgeo.org/mailman/listinfo/openlayers-dev
>>>>
>>>>
>>>>
>>>> --
>>>> Andreas Hocevar
>>>> OpenGeo - http://opengeo.org/
>>>> Expert service straight from the developers.
>>>>
>>>>
>>>
>>>
>>> --
>>> RICHARD Didier - Chef du pôle technique du Géoportail
>>> 2/4, avenue Pasteur - 94165 Saint Mandé Cedex
>>> Tél : +33 (0) 1 43 98 83 23
>>> _______________________________________________
>>> Dev mailing list
>>> Dev at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/openlayers-dev
>>
>> --
>> Andreas Hocevar
>> OpenGeo - http://opengeo.org/
>> Expert service straight from the developers.
>>
>
> --
> Andreas Hocevar
> OpenGeo - http://opengeo.org/
> Expert service straight from the developers.
>
>


-- 
RICHARD Didier - Chef du pôle technique du Géoportail
2/4, avenue Pasteur - 94165 Saint Mandé Cedex
Tél : +33 (0) 1 43 98 83 23


More information about the Dev mailing list