#1319 closed defect (wontfix)
this.inherited can cause undesired repeat calls to the same function
Reported by: | morris | Owned by: | sjmiles |
---|---|---|---|
Priority: | high | Milestone: | |
Component: | Core | Version: | 0.3 |
Keywords: | Cc: | ||
Blocked By: | Blocking: |
Description
Create a superclass with a function foo() that calls this.inherited() Create a subclass. Call subclass.foo() and the function foo() is called twice.
i.e.
dojo.declare('my.classes.root', null, {
tester: function() {
dojo.debug('root');
}
});
dojo.declare('my.classes.branch', my.classes.root, {
tester: function() {
dojo.debug('branch'); this.inherited('tester', arguments);
}
});
dojo.declare('my.classes.leaf', my.classes.branch, { });
var leaf = new my.classes.leaf(); leaf.tester();
Gives: DEBUG: branch DEBUG: branch DEBUG: root
The tester() function at the 'branch' level should only be called once.
Attachments (1)
Change History (3)
Changed 14 years ago by
Attachment: | test_inherited.html added |
---|
comment:1 Changed 14 years ago by
Resolution: | → wontfix |
---|---|
Status: | new → closed |
This is again due to a misunderstanding of inherited. I will have to bring the inherited problem to the dev list because it is causing problems.
Inherited was a zero-weight solution in the sense that it did not involve attaching any additional data to the class or method ahead of time (that wasn't already there as a result of dojo.inherits). It's useful for invoking certain inheritance chains, but the limitations are not intuitive.
In particular, when you call leaf.tester() the context is my.classes.leaf. There is no information that indicates this method is actually coming from the superclass. So the inherited call, begins looking up the inheritance tree from my.classes.leaf finding the first ancestral version of tester' in my.classes.branch prototype, which it invokes (again).
It's certainly possible to improve inherits, but in general this is problematic, since most of the core devs (including me) are not sanguine about forcing JS into a classical OOP model.
Run this in testslang to demonstrate issue.