On Feb 19, 12:04 pm, dashifen <[email protected]> wrote:
> Hacking around a bit, I have a working solution. Where I used to say
> something like this.responders.afterOpen = someOtherFunction() if I
> instead simply make it this.afterOpen = someOtherFunction() I can
> specify two different afterOpen actions for two different instances of
> a Dashbox object and it works fine. That'll at least let me continue
> with the project, but I must admit that I'm baffled regarding why the
> behavior is different. Any light shed on that would be greatly
> appreciated.
In short:
When you access `this.responders` from within a "subclass", it refers
to `responders` properties of a "superclass", since that is where it
is found during a property lookup. You end up assigning values to the
properties of the very same object.
In long:
When `this.responders.afterOpen` is evaluated, `responders` property
is being looked up on an object referred to by `this`. That object
(i.e. instance of `Dashdate` "subclass") has no such property and so
the next object in its prototype chain is being looked up -
`Dashdate.prototype`. `Dashdate.prototype` doesn't have such property
either, and so the lookup continues its way up the prototype chain,
until it finally finds `responders` on `Dashbox.prototype` (where you
inadvertently defined it with`Class.create`). The same process happens
to resolve `afterOpen` property of `this.responders`. In the end,
`this.responders.afterOpen` refers to a `Prototype.emptyFunction`
(that you defined earlier) and your assignment overwrites it with a
new value - `someOtherFunction`.
To have a per-instance `responders` object, you can create such
property on an object itself:
var Foo = Class.create({
initialize: function() {
...
this.responders = { ... };
...
},
...
});
To have a per-constructor (aka per-"class") responders object, you can
create it on constructor prototype's itself:
var Sub = Class.create(Super, {
...
responders: { ... },
initialize: function() {
...
}
...
});
The latter version is more memory efficient and is "shared" between
all of the instances of a "subclass".
--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---