Journaling on estrip is free and easy. get started today

Last Visit 2012-06-04 13:32:26 |Start Date 2006-02-27 14:30:33 |Comments 654 |Entries 407 |Images 203 |Sounds 3 |SWF 3 |Videos 44 |Mobl 11 |Theme |

Category: javascript

05/26/06 06:19 - ID#37328

call me

This post is extremely nerdy, so for those of you who don't care about Javascript's object model, have a good weekend, you may skip the rest of the post.

OK, so now that I'm talking to the nerds, here's some neat Javascript voodoo. I really like monkeying around with runtimes, and one reason that I like Python so much is that it lets you do all kinds of perverse things to the runtime with some very elegant effects. One thing it lets you do is define "callable" objects - objects that you can call like any plain old function. Classes, eg, are callable; this invokes the constructor and returns a new instance of the class. This maintains a similar syntax to other languages, except it's cleaner:

myInstance = MyClass() # python
var myInstance = new MyClass(); // Javascript
MyClass* myInstance = new MyClass(); // C++
MyClass* myInstance = [[[MyClass alloc] init] autorelease]; // Objective-C

This also makes for very elegant RPC calls. You can, eg, create a callable instance that you can use like a regular function, but takes care of all the grotty marshalling and connection management behind the scenes. Once you set it up, you can use the instance like any other function:

import xmlrpclib
server = xmlrpclib.ServerProxy('http://time.xmlrpc.com/RPC2')
print server.currentTime.getCurrentTime()

I'm writing a Javascript XML-RPC client library for work and I really wanted to be able to do something that elegant, like:

getCurrentTime = XMLRPCMethod('http://time.xmlrpc.com/RPC2', 'currentTime.getCurrentTime');
alert(getCurrentTime());

But in Javascript, you can only call things that are actual functions. However, since functions are first-class objects, you can bind your own attributes to them like any other object. You can access them from inside the body of the function by using the special variable arguments.callee:

function Greeter(yourName) {
    var result = function(timeOfDay) {
        alert('Good ' + timeOfDay + ', ' + arguments.callee.yourName);
    };

    result.yourName = yourName;
    return result;
}

greetZ = Greeter('Zobar');
greetP = Greeter('Paul');

greetZ('afternoon'); // 'Good afternoon, Zobar'
greetZ('evening'); // 'Good evening, Zobar'
greetP('evening'); // 'Good evening, Paul'

When I figured this out it blew my mind, and while I doubt its day-to-day practicality, I thought it's a handy tool for Javascripters to have. I also plan to make the XML-RPC library public, once I clean it up a bit, test it in MSIE, and finish the documentation.

- Z
print add/read comments

Permalink: call_me.html
Words: 391


Search

Chatter

New Site Wide Comments

joe said to joe
Never send a man to do a grandma's job...

sina said to sina
yes thank you!
Well, since 2018 I am living in France, I have finished my second master of science,...

paul said to sina
Nice to hear from you!! Hope everything is going great....

paul said to twisted
Hello from the east coast! It took me so long to see this, it might as well have arrived in a lette...