On Fri, May 22, 2015 at 1:34 PM, MRAB <pyt...@mrabarnett.plus.com> wrote: > On 2015-05-22 20:14, Laura Creighton wrote: >> >> The first time you discover that in javascript typeof(null) is 'object' >> and >> not 'null' you will scream. I wonder how many home versions of typeof >> to replace the system one exist out in the wild? >> > I don't think that typeof(null) should be 'null'. > > If the type of an integer instance is the integer type and the type of > a string instance is the string type, then the type of null should be > the null type, not a null instance. > > I suppose that you could consider that what JavaScript is doing is > equivalent to saying in Python that: > > None = object() > > like you sometimes do when you want a unique sentinel because None > itself would be an acceptable value.
If only it were that logical. null in Javascript is a primitive type. Here's what typeof returns on some other primitive types: js> typeof(4) "number" js> typeof(4.5) "number" js> typeof('hello') "string" js> typeof(true) "boolean" js> typeof(undefined) "undefined" An "object" in Javascript is basically just a collection of properties. For example: js> typeof([1, 2, 3]) "object" js> typeof({a: 1, b: 2, c: 3}) "object" Here's what happens when you try to access a property on null: js> null.foo typein:18:0 TypeError: null has no properties We can conclude that null is not an object. Even the MDN reference page for the typeof operator refers to this as "bogus". There was a proposal to fix this in ECMAScript 5.1, but it was rejected because it caused too much breakage. -- https://mail.python.org/mailman/listinfo/python-list