Archive for January, 2007


Thursday, January 25th, 2007

I just watched the first part of Douglas Crockford’s presentation “The JavaScript Programming Language,” available over at the Yahoo! User Interface blog. I’ve done a bit of JavaScript programming, but I’ve already learned a couple things I didn’t know before.

Doug recommends O’Reilly’s Javascript: The Definitive Guide with the praise, “I can say without fear that it is the least bad of a very large class of very, very bad books.” I’ll keep that in mind if I’m ever shopping for a JavaScript book.

The equals (==) and not-equals (!=) operators don’t behave as I expect them to. They do type coercion. So, 42 == 42 is true, as I expect, but "42" == 42 is also true, because JavaScript makes them the same type first. I think I’m going to start using === and !== instead, which behave as I expect ("42" === 42 is false, while 42 === 42 is true).

If you’ve ever user a language with C style syntax, you’ve probably used logical-and (&&) and logical-or (||). You can still do that in JavaScript, but the way it’s done is interesting. For A && B, if both A and B evaluate to true, then A && B evaluates to true. Here’s how JavaScript does that: If A is true, A && B evaluates to B. If A is false, A && B evaluates to A. It’s short circuit logic. If A is false, B isn’t even evaluated. You can think of && as the guard operator. A && B is then read: A guards B. With the guard operator,

if (A) {
  result = B;
} else {
  result = A;

can be written as, result = A && B.

The logical-or operator (||) is similar. As usual, A || B evaluates to true if either A or B is true, or if both of them are true. The way it’s calculated in JavaScript, A || B evaluates to A if A evaluates to true and B if A evaluates to false. So || can be thought of as the default operator. It defaults to A, but falls back to B if A is false. So,

if (A) {
  result = A
} else {
  result = B

can be written as: result = A || B.

And this is just funny: the typeof() function will give me the type of an object (i.e. typeof(42) is “number”). Using typeof(), we can see what type NaN (Not a Number) is. typeof(NaN) –> “number”. Not a number…is a number.

As a side note, Python treats and and or this way as well.

And moving into the very arcane, you can use this to simulate the C-style ternary operator in Python. In C/C++/Java/JavaScript: result = test?A:B. In Python: result = test and A or B.


Friday, January 12th, 2007

This is exciting. I just set up an OpenID identity with MyOpenID and set up to delegate. As soon as I can find a suitable Wordpress plugin and the time to install it, I’ll set it up here.

Simon Willison has a very nice screencast about OpenID.

Now I just need to find a place to use my fancy new OpenID…