Re: [Bindings] stringify

Hi Garrett.

Garrett Smith:
> Checking an object's toString return value to try and determine what
> it is is not a reliable way to check what type of object it is:
> toString() says nothing about an object's interface, other than the
> fact that, if no error is thrown, it supports toString().
> 
> For example:
> "[object Window]".toString()
> 
> Method toString is useful for providing a readable representation of
> an object's state. In ES, as David said, it's "[object " + [[Class]] +
> "]".

You can however use Object.prototype.toString.call() to ensure you are
getting Object’s version of toString(), and not some overridden version:

  >>> Object.prototype.toString.call("[object Window]")
  [object String]

You can rely on the string between “[object ” and “]” to be the
[[Class]] of the object.  (Well, assuming Object hasn’t been changed.)

-- 
Cameron McCormack, http://mcc.id.au/
 xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au

Received on Monday, 31 December 2007 03:19:53 UTC