D
dylan m. austin
Hello list. This is my first message and it's nothing needful but
rather playful. I'd just like to hear some thoughts on something I
consider to be a curiosity of javascript usage. I've seen a lot of
folks looking to implement some kind of super-like functionality in
javascript and it's usually baked into some larger effort of emulating
classes. However if you really need* that type of functionality why
not get it like so:
//assuming that foo has its own sayIt property and a sayIt property in
its prototype chain
var backup = foo.sayIt;
delete foo.sayIt;
foo.sayIt(); // now, delegates up the chain
foo.sayIt = backup;
So you can see that it's easy enough to make use of javascript's built-
in property delegation/lookup to emulate super-like functionality. And
if you wanted to make easier use of that pattern:
foo.supe = function( prop ){
var backup = this[prop];
delete this[prop];
if( typeof this[prop] == 'function' ){
var r = this[prop]();
}else{
var r = this[prop];
}
this[prop] = backup;
return r;
};
foo.supe('sayIt');
So there's an example of a general purpose super-like function. It
doesn't include argument passing even though that's entirely possible.
So is that not the simplest way of getting super-like functionality?
If anyone has some feedback I'd love to hear it. I hope I've been
clear.
You can also see a more complete working example of the code at:
http://highlight.tumblr.com/post/55510055/
* I haven't personally needed super-like functionality in my
javascript coding. Apparently, neither has Crockdolf as you can now
read at the bottom of his scroll of classical javascript teachings:
"I have been writing JavaScript for 8 years now, and I have never once
found need to use an uber function. The super idea is fairly important
in the classical pattern, but it appears to be unnecessary in the
prototypal and functional patterns. I now see my early attempts to
support the classical model in JavaScript as a mistake."
from: http://javascript.crockford.com/inheritance.html
rather playful. I'd just like to hear some thoughts on something I
consider to be a curiosity of javascript usage. I've seen a lot of
folks looking to implement some kind of super-like functionality in
javascript and it's usually baked into some larger effort of emulating
classes. However if you really need* that type of functionality why
not get it like so:
//assuming that foo has its own sayIt property and a sayIt property in
its prototype chain
var backup = foo.sayIt;
delete foo.sayIt;
foo.sayIt(); // now, delegates up the chain
foo.sayIt = backup;
So you can see that it's easy enough to make use of javascript's built-
in property delegation/lookup to emulate super-like functionality. And
if you wanted to make easier use of that pattern:
foo.supe = function( prop ){
var backup = this[prop];
delete this[prop];
if( typeof this[prop] == 'function' ){
var r = this[prop]();
}else{
var r = this[prop];
}
this[prop] = backup;
return r;
};
foo.supe('sayIt');
So there's an example of a general purpose super-like function. It
doesn't include argument passing even though that's entirely possible.
So is that not the simplest way of getting super-like functionality?
If anyone has some feedback I'd love to hear it. I hope I've been
clear.
You can also see a more complete working example of the code at:
http://highlight.tumblr.com/post/55510055/
* I haven't personally needed super-like functionality in my
javascript coding. Apparently, neither has Crockdolf as you can now
read at the bottom of his scroll of classical javascript teachings:
"I have been writing JavaScript for 8 years now, and I have never once
found need to use an uber function. The super idea is fairly important
in the classical pattern, but it appears to be unnecessary in the
prototypal and functional patterns. I now see my early attempts to
support the classical model in JavaScript as a mistake."
from: http://javascript.crockford.com/inheritance.html