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.)
No comments:
Post a Comment