Tag Archives: javascript


As you may know, Javascript is the world’s most misunderstood language. As a language it’s pretty nice – prototype OO, decent functional support, (almost) proper lexical scoping, etc. Some warts, but generally not too bad.

But dear god is the web browser an awful platform to develop for, and some of this is Javascript’s fault. The single threaded event model is a nuisance, it’s really hard to debug, and everything just gets in the way of everything else to produce really non-trivial interactions which break everything. Attempts to layer libraries on top which make this less awful are definitely valid, and to some extent work, but they just add new levels of incompatibilities and grossness.

This entry was posted in programming and tagged , on by .

Tail call optimisation in Javascript

I just happend across this old link

Basically it’s a (surprisingly elegant) way of creating tail call optimised functions in javascript.

There’s a slight subtlety with it unfortunately – you can’t simultaneously have a function call itself in a non-tail recursive manner. If you do it will break things.

It claims to support mutually recursive functions, and I think this is true, but I bet you’ll find there are some finicky details which can cause the stack to grow unboundedly before the optimisation kicks in, and I think you might have trouble properly optimising mutally recursive calls in this manner.

Still, very cool.

This entry was posted in programming and tagged , on by .

Javascript ‘with’ clause

I encountered a neat little feature in javascript recently.

Javascript has things that look like global variables. They’re not. What they *actually* are is fields of the global object. The following syntax lets you write a bunch of code that uses a different global object:

// do Stuff

Has foo as the global object inside the braces.

Well, almost. There’s a caveat.

Try the following in Rhino or Spidermonkey:

foo = { bar : 2 };

with (foo)
baz = 3;


It will print “undefined”.

But if you do


It will print 3.

Huh? What’s going on?

This is how javascript scope works. The assignment “foo = bar” will not find the identifier ‘foo’ bound, so will fall outwards into enclosing scopes until it finds a scope which does bind it, stopping at the top level scope (and corresponding global object) if it doesn’t find any.

It’s kindof annoying, but it does make sense. Further it’s more or less how it has to work given the combination of Javascript’s soft objects and lexical scoping.

With this caveat borne in mind, the ‘with’ method is quite cool. I shall try and find an excuse to use it in future. :-) (I encountered it via Chickenfoot, where it seems to be useful for loading other pages in the background and applying chickenfoot methods to them).

Update: Apparently this is deprecated and considered bad practice, as it’s difficult to optimise and leads to some non-intuitive issues with scoping. Oh well.

This entry was posted in programming and tagged on by .

Cool gadget of the day: Chickenfoot

You’re probably not use the Chickenfoot Firefox extension. You really should be. It’s basically an extremely cool API and plugin for writing user scripts (similar to Greasemonkey, only awesome), with very clever pattern matching for finding the relevant elements on the page, which has always been the most annoying thing with writing user scripts.

I originally found it via digging through google techtalks. The original techtalk is here (it’s quite long).

Also, here’s a useful snippet for making it less annoying to use from the keyboard. It’s not mine – the first part is due to Rob Miller and the second is a trivial adaption of the first. It is however really rather handy.

This entry was posted in programming and tagged , , on by .

Factoid of the day: Javascript command line

If you install the firebug firefox plugin, it provides you with a command line allowing you to execute arbitrary javascript in browser window in a nice friendly manner.

This entry was posted in programming and tagged , on by .