Hi all
Just thought I'd share something I had a little fun hacking together
tonight: https://github.com/skilldrick/funcobj
I wanted to see if I could create an inheritance system in JavaScript
only using closures. I've not allowed object literals or the dot
operator, which means method lookup happens in switch statements.
I'd be really interested to see what you guys think, and if there's
anything I've done that's stupid (apart from the whole project).
This is the guts of the system:
//methodsInitializer: a function that returns methods for the new object
//initArgs: initialization arguments for the methodsInitializer
//superObject: an optional object to inherit methods
function objMaker(methodsInitializer, initArgs, superObject) {
var methods = apply(methodsInitializer, initArgs);
function dispatch(methodName, self) {
self = self || dispatch; //if self given use it, otherwise use this function
var dispatchArguments = arguments;
var method = methods(methodName);
if (method) {
return function () {
return applyWithSelf(method, self, arguments);
};
}
if (superObject) { //re-call with superObject (this can happen recursively)
return function () {
//when calling super, make sure self is set to the method receiver
return apply(superObject(methodName, self), arguments);
}
}
log("Method", methodName, "not known");
}
return dispatch;
}
--
Nick Morgan
http://skilldrick.co.uk
@skilldrick
Save our in-boxes! http://emailcharter.org
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]