5

When I do this:

Object.prototype.toString.call( null )

or this:

Object.prototype.toString.call( )

the browsers return the following string value (in both cases):

Chrome 9: [object global]
Safari 5: [object DOMWindow]
Firefox 3.6: [object Window]
Opera 11: [object Window]

However in IE9 RC the return values are [object Null] and [object Undefined] respectively.

The ES5 specification states:

NOTE The thisArg value is passed without modification as the this value. This is a change from Edition 3, where a undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value.

See here: http://es5.github.com/#x15.3.4.4

Does this mean that IE9 is the only browser that follows this newly introduced rule?

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • Great observation! May I ask how you came across this? I guess this means that you have to check for null before using call()... – Mark Eirich Feb 20 '11 at 04:38
  • @Mark Well I've read patrick's answer here: http://stackoverflow.com/questions/5054352/why-use-typeof-for-identifying-a-function/5054416#5054416 and than started to pass in all kinds of values into `Object.prototype.toString.call` to test the cross-browser compatibility of retrieving the `[[Class]]` internal property of an object. Check out my observations here: http://vidasp.net/javascript-internal-class.html – Šime Vidas Feb 20 '11 at 04:42
  • Looking at your test, I was surprised to see that JSON was reported as `Object`, especially since Math was reported properly. So I thought maybe jsFiddle was overwriting the native JSON implementation. Sure enough, testing it in the console while StackOverflow is loaded, I get `[object JSON]`. – user113716 Feb 20 '11 at 14:18
  • ...Correction, looks like MooTools is the culprit. Testing with no library loaded gives the proper result. http://jsfiddle.net/e4cP9/6/ – user113716 Feb 20 '11 at 14:27
  • @patrick Cool, I didn't know about the "No-library" option. – Šime Vidas Feb 20 '11 at 22:17

1 Answers1

3

I'm going to go out on a limb and say yes. ;)

FYI, I just tested FF4, and it gives [object Null] and [object Undefined]

user113716
  • 318,772
  • 63
  • 451
  • 440
  • 1
    Yes, FF4 and IE9 it is. The other browsers should follow soon. Funny though how Google released 6 versions of Chrome since the ES5 publication, and still nothing `:)` – Šime Vidas Feb 20 '11 at 04:38
  • Šime Vidas: Yes, I'm surprised by that. When I saw your question, I upgraded to Chrome 10 beta, and still the same. – user113716 Feb 20 '11 at 04:40