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
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
Permalink: call_me.html
Words: 391
Author Info
Date Cloud
- 10/11
- 08/10
- 07/10
- 06/10
- 05/10
- 04/10
- 03/10
- 02/10
- 01/10
- 12/09
- 11/09
- 10/09
- 09/09
- 08/09
- 07/09
- 06/09
- 05/09
- 04/09
- 03/09
- 02/09
- 01/09
- 12/08
- 11/08
- 10/08
- 09/08
- 08/08
- 07/08
- 06/08
- 05/08
- 04/08
- 03/08
- 02/08
- 01/08
- 12/07
- 11/07
- 10/07
- 09/07
- 08/07
- 07/07
- 06/07
- 05/07
- 04/07
- 03/07
- 02/07
- 01/07
- 12/06
- 11/06
- 10/06
- 09/06
- 08/06
- 07/06
- 06/06
- 05/06
- 04/06
- 03/06
- 02/06
Category Cloud
More Entries
My Fav Posts
- This user has zero favorite blogs selected ;(
Considering the number of people who are having success with OO javascript, perhaps I should try things again and give the language one last chance to redeem itself.