We have as clear a notion of the substance of spirit as we have of bodyJohn Locke
Relates to DOM Scripting
Writing some Javascript the other day I found myself wanting to override a method of an object's prototypical object while still having access to the inherited method. That is, in class based terminology, I wanted to be able to invoke the overriden method of the superclass. In Java this is achieved with the keyword super which provides a reference to the current object as an instance of its superclass. However, as a prototype-based language, Javascript does not provide such a reference. Kevin Lindsey demonstrates the recursive dilemma and suggests an idiom for simulating constructor chaining by utilising the ECMA compliant call method. While this could serve my purpose, I decided to look for a way to create a single reference to access overriden member variables and methods (just like super).
Here is one possible solution:
Object.prototype.parent = function(type) {
var p = new Object();
for (x in type.prototype) {
if (x != "parent") {
p[x.toString()] = this[x.toString()];
}
}
return p;
}
function BoxContainer(param) {
Container.call(this, param);
var parent = this.parent(BoxContainer);
this.service = function() {
if (this.service.arguments.length == 0) {
return parent.service();
}
else {
// .. do some stuff ..
return stuff;
}
}
}
BoxContainer.prototype = new Container;
The prototype object, Container, is that used by Douglas Crockford in his article Private Members in Javascript.
A new method, parent, is added to the prototype property of the Object object. This method allows an temporary object to be filled with the public access variables and methods of a prototype before they are overriden. So, the constructor function, BoxContainer, invokes this method passing its prototypical object. The method returns a new object reference filled with all the properties and methods of the prototypical object, and this is assigned to a private variable, parent. Then a closure allows the inner function service to maintain access to parent after the constructor function BoxContainer has returned. (NB while super may have been an elegant choice for the identifier, it is a reserved word, so I choose to use parent - not to be confused with DOM!)
Posted on Friday, Oct 08, 2004 at 03:44:50.
Comments on Super Simulation in Javascript OOP (6)
α comment
I never use the prototype keyword in Java Script, thats just asking for trouble. Using the prototype syntax causes alot of problems when mimicing more advanced OO features, plus the syntax is incredibly goofy.
Here is how I do method overriding in Java Script:
That will alert the string "Base method." and then the string "Child method." No extended javascript features are used here, so this works with java script versions as old as they get.
Posted by Paul Young
Sunday, Dec 05, 2004 at 02:41:56
β comment
Well, the message board messed up the spacing, hopefully you can make it out.
Posted by Paul Young
Sunday, Dec 05, 2004 at 02:43:08
γ comment
It's also not well supported in IE…
you can't prototype Object or HTMLElement, which is sad :-(
Posted by momos
Wednesday, Feb 09, 2005 at 09:10:59
δ comment
Paul: But Javascript is founded on a prototype-based object model. This was really just an exercise in simulation of one feature from class-based object-oriented languages using prototyping.
Still, see where you are going with your sample. I have code wrapped it so it is clear now :)
Momos: Not the only unsupported feature hey! ;)
Posted by Tom
Sunday, Mar 20, 2005 at 20:01:44
ε comment
The author of "Private Members in JavaScript" is Douglas Crawford, not David Crawford.
Posted by Al Vazquez
Wednesday, Jul 27, 2005 at 20:49:01
ζ comment
Thanks very much, the first comment it's very good. I used this.superMethod = this.Method; this.Method = function() { this.superMethod() …. }
Posted by Zusuma
Friday, Dec 16, 2005 at 18:15:55