$ js
Rhino 1.7 release 1 2008 03 06
js> var foo = new RegExp("([a-zA-Z/])+");
/([a-zA-Z/])+/
js> foo.test(null)
true
What? Passing null to a regexp passes?
$ js
Rhino 1.7 release 1 2008 03 06
js> var foo = new RegExp("([a-zA-Z/])+");
/([a-zA-Z/])+/
js> foo.test(null)
true
What? Passing null to a regexp passes?
While trucking away at $work, Jim pointed out a subtle nasty edge case in how JavaScript and the browser interact. Let’s say you have the following code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>shared var</title>
<script type="text/javascript">
var foo = "awesome";
bar = "bartastic";
window.baz = "bazzoom";
</script>
<script type="text/javascript">
if(!window.foo) var foo = "not awesome";
if(!window.bar) var bar = "not bartastic";
if(!window.baz) var baz = "not bazzoom";
</script>
</head>
<body>
<p>Foo is <script type="text/javascript">document.write(foo);</script></p>
<p>Bar is <script type="text/javascript">document.write(bar);</script></p>
<p>Baz is <script type="text/javascript">document.write(baz);</script></p>
</body>
</html>
I think most folks would expect the output to be:
Foo is awesome
Bar is bartastic
Baz is bazzoom
and it is on Firefox, Safari and Opera, but on IE you get:
Foo is awesome
Bar is bartastic
Baz is not bazzoom
Care to guess why?
Bjorn worked up a pretty sweet scanner for SCons that understands dojo.require statements. If you’re using SCons and Dojo, definitely take a peek.
In the coming months, all of the major browser vendors are going to implement a new API called querySelectorAll, which allows us web developers to query a document for elements matching a CSS3 selector and quickly get back a list of matching nodes. This is really fantastic news and should help to speed up one of the more common things we do in web apps.
We have basic support for the API in the latest Firefox 3.1 nightlies, IE8 beta 2, and WebKit, and Dojo has basic support in trunk for using querySelectorAll to drive dojo.query if it’s available, so I thought I’d try it out and see what happens.
Well, things … mostly … work. The selection stuff is great, but Dojo does one rather nice thing with the dojo.query interface: it returns an Array object that’s been decorated with the JS 1.7 array methods if they’re not natively available, plus some other Dojo-specific things, like connect and style and so forth, to make running operations against the result set easy. For some reason, a number of these methods were broken.
I did some digging and it turns out that the breakage is happening because the querySelectorAll return is not an Array or a subclass of an Array, but a new thing called a StaticNodeList. It looks mostly like an array and works like an array, but it doesn’t have any of the new array methods (at least on Firefox 3.1 and IE8 beta 2) and as it’s not an array, it fails an instanceof check. This means code like this:
var spans = document.querySelectorAll("span");
var divs = document.querySelectorAll("div");
var both = spans.concat(divs); // fails, no concat method
just plain doesn’t work. JS devs already have this array-ish problem with the return from document.getElementsByTagName and the arguments object available inside functions. From looking at the spec for the StaticNodeList, I cannot for the life of me see why this isn’t just a plain array with the standard array methods on it. It’s a static collection, so treating it just like an array should be fine, no?
If anyone can shed light on why the returned value from querySelectorAll is not just a plain JS array, I’d love to hear it.
I’ve been spending the last few nights reading Michael Ruhlman’s The Making of a Chef. I’ve enjoyed it immensely; I’d always wondered what it was like inside that campus after driving past it so many times in our weekend adventures northward from Tarrytown. Mandy and I never got the chance to eat at any of their restaurants while we lived downstate, but now we’re hopefully planning a trip south to sample the cuisine. Ruhlman is a pleasure to read and does a fantastic job conveying the passion and pressure of going through the Culinary’s course load. If you’re into cooking at all or want to know more about what it’s like to be inside the Culinary Institute of America (our other CIA), I heartily recommend the book.
Last night, as I was nearing the end of the book, I came across a scene that I’ve been replaying in my head all day. When you’re cooking in a professional kitchen, things move very fast and you have to come into it prepared and once there you have to be incredibly efficient to stay on top of all the orders coming in. When you get behind or overwhelmed, it’s called being “in the weeds.” While working at American Bounty, the premier restaurant at the Culinary and the last stop for the students before graduation, Ruhlman is working the grill station and another student is lost in the weeds. He’s getting behind and his station is getting messier and messier, enough so that the head chef asks him repeatedly to clean up his station. After a few such reminders, the instructor finally stops the student and tells him how he likes to get out of the weeds.
He takes a moment. He wipes down his station, getting it perfectly clean. He arranges everything he needs back into order, and this little restoration of order helps him clean up his mind as well. When you’re in the weeds, it’s as much a mental problem as it is a physical one. To perform quickly and efficiently, you have to be focused. The mess clouds your focus and that lack of clarity shows up in the final result.
This reminded me a lot of programming under a tight deadline. In the rush to get everything done and out the door, you tend to get messy. You don’t write tests, you don’t verify that code changes work in all the browsers, you check code in without actually compiling it first, and eventually the whole thing disintegrates. In your effort to go faster, you get sloppy and you end up slowing down.
So, a reminder to myself. When things get hairy, take a moment and clean everything up. You’ll probably get done sooner as a result.