If I’m in the constructor of an extended mootools class, it’s simple to call the parent:
var Animal = new Class({
initialize: function(name, race) {
this.name = name;
this.race = race;
},
doSomething: function() {
console.debug('make a sound');
}
});
var Cat = new Class({
Extends: Animal,
initialize: function(name) {
this.parent(name, ‘cat’);
}
});
What if you want to call a parent method? The reply lies in the following line of the Extends mutator:
self[key]._parent_ = previous;
So, here’s a small class to use with Implements to have a nice access to the parent methods:
var ParentCall = new Class({
_parent: function(name) {
return this[name]._parent_.bind(this);
}
});
And here we have a brand new cat which uses this:
var Cat = new Class({
Extends: Animal,
Implements: ParentCall,
initialize: function(name) {
this.parent(name, 'cat');
},
doSomething: function() {
this._parent('doSomething')();
console.debug('a "meow", to be exact.');
}
});
Posted by mattia as Uncategorized at 12:58 PM CEST
