[OpenLayers-Dev] Constructor trouble when testing new control
Christopher Schmidt
crschmidt at metacarta.com
Fri Oct 12 11:54:01 EDT 2007
On Fri, Oct 12, 2007 at 06:46:36AM +0100, Ian Mayo wrote:
> Sorted. Will collate patch at next opportunity.
>
> I suspect I still haven't got the hang of OpenLayer javascript
> constructors- but I know the correct model to plagiarize next time...
When you subclass from something in Python, your class looks like this:
class SomeOtherClass(object):
def __init__(self, options):
# Do Stuff with Options
class MyClass(SomeOtherClass):
def __init__(self, myclass_specific, options):
SomeOtherClass.__init__(self, options)
That is:
SomeOtherClass initializer takes one argument (other than itself),
but MyClass takes more arguments. When MyClass calls the
SomeOtherClass init, it should only pass the arguments to
SomeOtherClass that SomeOtherClass is expecting, but not the
additional args that MyClass cares about.
This is the same in OpenLayers Javascript subclassing:
Control = OpenLayers.Class({
initialize: function(options) {
}
});
Control.Subclass = OpenLayers.Class({
initialize: function(additional_arg, options) {
Control.prototype.initialize.apply(this, [options]);
}
});
The 'apply' is saying 'call this function on *this* object, passing
*this* list of arguments'. 'arguments' itself is a special keyword in
Javascript, meaning 'an array of all args passed to this function'.
Since the "Control" initialize only takes the one arg, instead of
passing all the args we were given, we pass only the one.
Does that help it make more sense?
Regards,
--
Christopher Schmidt
MetaCarta
More information about the Dev
mailing list