Wednesday, August 29th, 2007

JavaScript tips for rookies and gurus

Ars Technica has good piece aimed at helping developers of all JavaScript experience levels gain valuable knowledge. Here’s a tidbit from the article:

Guru: polymorphic callable objects

Supposing you were writing a type system, you might want objects of all your types to have the option of being callable, and for the call behavior to be redefinable in sub-types. One fun way to do this is to always use Function instead of Object as your base type.

var callableType = function (constructor) {
return function () {
var callableInstance = function () {
return callableInstance.callOverload.apply(callableInstance, arguments);
constructor.apply(callableInstance, arguments);
return callableInstance;

As one commenter mentioned, couple this article with Douglas Crockford’s YUI! video, “The JavaScript Programming Language”, and you’ve got some good information flowing:

Posted by Rey Bango at 6:30 am

4 rating from 25 votes


Comments feed TrackBack URI

I’ll just come out and say it. I read TFA and I still have no idea what this “polymorphic callable objects” code is actually doing. Can anyone else unravel the mystery for me? Is it good enough to make its indecipherable code worth while?

Comment by Joshua Emmons — August 29, 2007

I think the secret is in the callOverload function, which has not been explained/defined. I guess it works because functions have properties, similar to Objects?? I would love to see it explained in more detail.

Comment by Charles — August 29, 2007

The idea is that user defined types could add a this.callOverload function to their instances from within their constructor. Function objects can be used as “dictionary” objects just like normal Objects.

this.MyType = callableType(function () {
/* herein, "this" is your callableInstance */
this.callOverload = function (...) {
return ...;
var callableInstance = MyType(); /* instantiates a callable instance */
callableInstance(); /* would implicitly call your user defined callOverload */

I’ll have to blog a more thorough explanation.

Comment by Kris Kowal — August 29, 2007

Small clarification: JavaScript functions are not just *like* objects, they *are* objects. typeof (function () {}).prototype === ‘object’. You can use functions in JavaScript just like any other value (strings, integers, etc.). It’s an extremely powerful feature of the language.


Comment by Pyrolupus — August 30, 2007

So if I understand correctly, the “callable instance” crud is merely to facilitate shallow syntactic sugar, e.g.:

// regular object constructor
var SomeType = function () {
this.f = function () {
return 123;
this.callOverload = f;
// special constructor that makes the object "callable"
var SomeCallableType = callableType(SomeType);

var x = new SomeType();
var y = SomeCallableType();

alert(x.f() === y());

So the result is that you can pick one “default method” to call when the object is called as if it were a function. As you can see, in the example above I chose to pick f as the “default method”, and the “callable instance” crud saved from having to type “.f”.

I agree that function juggling can be fun, but I honestly don’t think this is worth the effort.

Comment by Tim Cooijmans — August 31, 2007

(Obviously, “this.callOverload = f” should have been “this.callOverload = this.f”.)

Comment by Tim Cooijmans — August 31, 2007

Leave a comment

You must be logged in to post a comment.