Re: [Bindings] stringify

On Dec 30, 2007 7:19 PM, Cameron McCormack <cam@mcc.id.au> wrote:
>
> 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]
>

Yeah, that is true.

But not 100% foolproof:
Function.prototype.call uses the calling context when arg0 is undefined or null.

Object.prototype.toString.call( undefined )
Object.prototype.toString.call( null )


> 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
>
>



-- 
Monkey, so they say, is the root of all people today.

Received on Monday, 31 December 2007 09:34:57 UTC