On 10.08.2011 12:28, Nick Morgan wrote:
Yes, definitely inspired by SICP (though I wanted to add inheritance
as well). I'm sure I'm re-implementing a lot of what JS does under the
covers - I just wanted to see if I could do it without objects. That's
interesting to know though.
It's good to note and understand that this implementation (with using
function abstractions) reflects only _ideological_ part of
"message-passing". In reality of course these objects are implemented in
much more efficient way (with effective, dense, memory allocation and
effective handling of passed messages to dispatcher; e.g. see [[Get]]
internal method in ES spec which is exactly the dispatcher you've
implemented with functions).
The one thing that this code adds is the option to do some kind of
`method_missing` in the method definitions, which is something I miss
in JS.
Yes, that's the thing of the "message-passing" style in the dynamic
dispatching systems
(http://dmitrysoshnikov.com/ecmascript/chapter-7-1-oop-general-theory/#delegation-based-model)
-- a special system signal (method_missing, __noSuchMethod__,
doesNotUnderstand, __call, etc.), which dispatcher can activate in case
if an object cannot reply to a message. In this case, the dispatcher can
try to redirect the request to another route (that is, try to find the
property/method in another inheritance chain).
In SpiderMonkey as an extension there is this __noSuchMethod__.
Using proxies, you may trap all needed methods implementing your own
dispatcher, i.e. [[Get]] method via `get` property of the proxy handler.
In static systems (with "method-calls" technique), usually static
compile-time vtables are used to handle method calls, and it's not so
easy to handle new method which is added dynamically at runtime to
object. OTOH, static systems are much faster in this respect.
Dmitry.
Though if that was the only thing I wanted to achieve, I'm sure
I could do it a lot more easily another way!
I'll look into not creating a new FE in the dispatcher.
Cheers!
Nick
On 10 August 2011 08:46, Dmitry A. Soshnikov<[email protected]> wrote:
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]
--
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]