Javascript Object Promotion
→ 19 February 2010.
Tags:
javascript,
programming
So it’s not something you should have to worry about (often), but I just spent an inordinate amount of time figuring out exactly why a particularly innocuous-looking piece of code was failing. It’s also yet another surprising part of the javascript language, and if you often deal with javascript then it’s in your best interests to keep track of such oddities (there are a lot of them!).
To illustrate, consider the following array intersection function:
var list1 = ["a", "b", "c"];
var list2 = ["a", "b", "d"];
function intersect(a,b) {
// find the intersection of two arrays
var intersection = [];
jQuery.each(a, function() {
if(b.indexOf(this) != -1) {
intersection.push(this);
}
}
return intersection;
}
You might expect calling intersect(list1, list2) to return ["a", "b"], since those are the common elements in the two sets. However, what you really get is [].
The problem is that I was a bit lazy, and used jQuery.each() to iterate over my collection (I hate the for x in y construct, and for loops are just so C). The each method calls your provided function on each member of the provided array, setting this in the context of each call to the current array element.
But it turns out that when you call a method on a primitive string object, the javascript (well, ECMAScript really) language specifies that this call takes place not on the string primitive type, but on the String object type (it’s kinda like a boxed value in Java). That is, fun.call("some string") actually ends up as if you had written fun.call(new String("some string")).
The String and string objects will compare equal when using the “==” operator – but since they are of different types, they will not compare as equal using the “===” operator. Evidently that is the type of equality that indexOf() uses, therefore none of the String objects will ever appear in the array of string primitives.
Normally, you will neither notice nor care. However, when extending the String class or calling a function with a primitive type as the subject, you should keep this conversion in mind. Note that the conversion does not apply to function arguments, only to the subject of a function application (i.e this).
I'm continually amazed by the high quality answers found on stackoverflow.com – I posted this question asking why the object promotion occurs (and if that is indeed what’s going on), and was rewarded with an explanation and a link to the precise part in the ECMAScript spec where the behaviour is detailed.
…
Bluetile: A friendly tiling window manager
→ 13 February 2010.
Tags:
linux
I've recently started using bluetile instead of compiz, and I think I'm going to stick with it. Bluetile is a tiling window manager, but it’s not like all those other tiling window managers*:
- it’s simple
- it’s easy to learn
- it’s built with GNOME support out of the box
- it still supports direct manipulation style using the mouse if you just want to change a window’s size, or swap window locations. I think this is crucial for keeping it accessible to new people like myself.
* well okay, it is like those other tiling window managers. Specifically, it’s built on xmonad. But it presents a simple veneer over the vast complication that is xmonad (I should know, I tried to configure it just last week ;)).
Bluetile is, sadly, notoriously difficult to install. But there are good guides around, I used this one for ubuntu karmic.
The good news is, bluetile has recently been merged back into the xmonad mainline. And xmonad is apt-gettable (at least on ubuntu). So in theory, the next release of xmonad will make it pretty easy to run in bluetile mode, without having to compile-your-own-window-manager (which certainly does not make for a gentle learning curve). If anyone is considering playing around with alternate window managers, it’s definitely worth your while to give bluetile a go.
…
Making sense of async.js
→ 12 February 2010.
Tags:
javascript,
programming
async.js is a fascinating library for taming asynchronous javascript. It’s highly experimental, and as far as I know, it only works in firefox. But the idea is an important (and useful) one, so I think it’s definitely worth knowing about.
There are a few approaches to dealing with asynchronous javascript:
Compile [some language] to javascript
This is the approach taken by GWT, pyjamas, and many others. It’s usually extremely heavyweight, so it mainly makes sense for big apps.
Compile [almost-javascript] to javascript
Most notably this includes Narrative Javascript and its successor, Strands. This is a reasonable approach, but the lack of maturity / tool support makes debugging extremely hard. Also, I ran into a number of bugs in both these libraries. The thought of finding and fixing more of those bugs is not at all fun.
Write a javascript library
This is doable, but typically looks hideous, convoluted, and is usually quite burdensome to try and use.
Async.js is the most plausible attempt at #3 that I've seen. It uses a reasonably clever (but not unknown) trick to to turn Javascript 1.7’s generator functionality into an event-based coroutine system. Specifically, this allows for a program to “wait” for a callback, while not actually blocking the javascript interpreter (as a synchronous AJAX call would). Go read the async.js page to learn more, because the following isn’t going to make much sense if you don’t know roughly how to use async.js.
…
Javascript's fragile "this" statement
→ 05 January 2010.
Tags:
javascript,
programming
Javascript’s this statement must be the most fragile and confusing statement I've come across. Many languages
have the concept of “this”, but none mess with it to hard as javascript does. As I have recently discovered,
there are two fundamental issues with javascript’s this:
this does not get captured when storing an object member into a function object
- you can never actually be certain what
this will be
For object-oriented programming, these two facts are entirely terrifying. Let me illustrate each one:
1. this does not get captured when storing an object member into a function object
function Obj() {
this.method = function() {
return "method() called - I am " + this;
};
this.toString = function() {
return "[Obj instance]";
}
}
Now, consider the following scenarios:
var obj = new Obj();
obj.method();
// returns "method() called - I am [Obj instance]"
var obj_method = obj.method;
obj_method();
// returns "method() called - I am [object DOMWindow]"
What happened to this? I can find no explanation anywhere as to why it has been lost (and the window object
used in its place), but it is consistent. This may not seem like a big deal, but even aside from the
awkwardness, it’s completely non-obvious – and therefore a great candidate for sneaky bugs.
2. You can never actually be certain what this will be
Some see this as a feature, and it is in some cases. But the fact remains that the caller of any function can
set this to be any object they choose is cause for great suspicion on the part of any callback code.
What’s worse, as a library writer there are cases where it’s impossible to not mess with the value of this.
Other languages have the concept of a unsplat operator. Google it if you don’t know this term, but basically
it will turn a list of objects into an argument list. That is, func(1,2,3) is the same as func(*[1,2,3])
(where * is the un-splat operator). This is very important for higher-order / functional programming, where you might
write a proxy function that wraps a normal function call with some useful behaviour.
Anyways, javascript does have an unsplat operator. Kind of… The following code will work:
function call_other() {
var _arguments = Array.prototype.slice.call(arguments);
var func = _arguments[0];
var func_args = _arguments.slice(1);
// do whatever proxy stuff you need to do here
func.appy(null, func_args);
}
Except for that first parameter to the apply function. Whatever you pass in there is what this will be set
to in the context of the called function. With no discernible means of extracting what this would normally
be for the given function, it becomes impossible not to clobber the otherwise extremely-useful this
statement.
…
A Couple of Drawings
→ 28 December 2009.
Tags:
image,
illustration
A while ago Nys asked me to draw her a bookmark. It took me a while
to think of anything, but I eventually created an
anatomically-incorrect bookworm (with arms and all), who munches on
the top of your book whenever you close him up inside it.
I also had an old apple sticker laying around, from before I
switched to linux. I stuck it on my current case, but it
seemed a little out of place. So I modified it a bit. This would
make much more sense if I actually had a hackintosh, but it’s
still cute ;)

…
→ 27 December 2009.
It’s a friggin monkey! Nursing snow tiger cubs!