Wednesday, May 30, 2007

This Blog Has Moved (and XRegExp)

Blogger was a nice and easy introduction to the world of blogging, but alas, the time has come to move on. I now have my own domain name, and a shared hosting provider which supports both PHP and ColdFusion. Woohoo! Here's my new JavaScript / regex blog.

For feed subscribers, please update the this blog's feed URL to point here, as I'll no longer be posting on blogspot. In addition to a fancy new extended JavaScript regular expression constructor (XRegExp) which I've already posted over there, most of the stuff from here has been migrated, and in the process I've updated several posts and added demos for a few more, including Leet Translator, REMatch, and both the ColdFusion and JavaScript implementations of parseUri. Check 'em out.

Thursday, May 10, 2007

Fun with JavaScript variable types and constructors

Try running the following code in Firebug (the results are shown in trailing comments):

console.log(typeof null); // object
console.log(null instanceof Object); // false
console.log(typeof [1,2,3]); // object
console.log(typeof /regex/); // function in Firefox; object in IE
console.log(typeof new String()); // object
console.log(typeof Object); // function
console.log(Object instanceof Object); // true
console.log(Object instanceof Function); // true
console.log(Function.constructor); // Function()
console.log(Function.constructor.constructor); // Function()
console.log(window.constructor); // function() [note the lowercase "f"]
console.log(window.constructor.constructor); // Object()
console.log(window.constructor.constructor.constructor); // Function()
console.log(Function()); // anonymous()

console.log(typeof NaN); // number
console.log(NaN.constructor); // Number()
console.log(NaN instanceof Number); // false
console.log(NaN == NaN); // false
console.log(null + 1); // 1
console.log(null + null); // 0
console.log(undefined + 1); // NaN
console.log(null + "string"); // nullstring
console.log(undefined + "string"); // undefinedstring
console.log({} == 0); // false
console.log([] == 0); // true
console.log(1.0.toFixed(2)); // 1.00

console.log(new Boolean(false) == false); // true
console.log(new Boolean(false) === false); // false

Surprised by any of those results? If not, you're probably either quite knowledgeable about JavaScript variable types, type conversion, and constructors, or you don't fully understand some of the peculiarities and seeming contradictions. (If you have any questions, feel free to ask in the comments.)