[OpenLayers-Users] Overwrite and extend initialize function of a class

Eric Lemoine eric.lemoine at camptocamp.com
Tue Feb 28 00:44:56 EST 2012


On Wednesday, February 15, 2012, Armin Burger wrote:

> Eric
>
> thanks. I saw some postings for this yesterday but did not get it to work,
> and thought it might not be fully functioning... Now I tried again - and
> succeeded... ;-)
>
> I could overwrite the function by adding the complete original code and
> afterwards the lines I needed for extending it. But I did not find any
> possibility to make a call to the original initialize function and then
> afterwards just the few lines to extend the function. But maybe this is not
> possible


You can do this:

var f = A.prototype.f;
A.prototype.f = function() {
    f.apply(this, arguments);
};

Or, using the overwrite function given in the tests:

var f = A.prototype.f;
A = overwrite(A, {
    f: function() {
        f.apply(this, arguments)(
    }
});


Anyway, it's probably safer to subclass OpenLayers classes rather than
monkeypatching them like this.


-- 
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120228/bce2733c/attachment.html


More information about the Users mailing list