So you've implemented "message-passing" style objects (I guess, from
SICP, by looking on the code). The thing is, that objects in JS exactly
work by this technique and all this desugared variants are sugared into
object literal notations, dot and bracket property accessors (i.e.
message passing to objects).
That's interesting for understanding this technique (in contrast with
say "method calls" technique in static languages, when you cannot change
dispatching routs, inheriting e.g. from several delegates). Though, you
may want to consider not creating every time a new FE on each message
acceptor in the dispatcher.
Dmitry.
On 10.08.2011 2:25, Nick Morgan wrote:
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;
}
--
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]