Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Object.prototype.toString(): directly invoking vs. using call() --why different results ?

Reply
Thread Tools

Object.prototype.toString(): directly invoking vs. using call() --why different results ?

 
 
javadesigner@yahoo.com
Guest
Posts: n/a
 
      04-06-2009
var a = [4];

Object.prototype.toString(a); //-> [object Object]
Object.prototype.toString.call(a); //-> [object Array]

Why are the two different ? Can someone post a detailed explanation ?

Best regards,
--j

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-07-2009
On Apr 7, 9:36 am, (E-Mail Removed) wrote:
> var a = [4];
>
> Object.prototype.toString(a); //-> [object Object]
> Object.prototype.toString.call(a); //-> [object Array]
>
> Why are the two different ? Can someone post a detailed explanation ?


Because according to ECMA-262, the following occurs when calling
Object.prototype.toString as a method:

| 15.2.4.2 Object.prototype.toString ( )
|
| When the toString method is called, the following steps are taken:
|
| 1. Get the [[Class]] property of this object.
| 2. Compute a string value by concatenating the three strings
"[object ", | Result(1), and "]".
| 3. Return Result(2).

So when processing:

Object.prototype.toString(a);

The this keyword is a reference to Object.prototpye, which is an
Object (its [[class]] is Object), so the result [object Object] will
result regardless of the argument provided (even none at all).

When you use:

Object.prototype.toString.call(a);

the call method sets toString's this keyword as a reference to a, an
Array, so you get [object Array].

Incidentally, this might be the ultimate "isArray" test as it provides
access to the internal [[class]] property.


--
Rob
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-07-2009
On Apr 7, 11:00*am, RobG <(E-Mail Removed)> wrote:
[...]
> Incidentally, this might be the ultimate "isArray" test as it provides
> access to the internal [[class]] property.


Forgot to add that this was covered here:

<URL: http://groups.google.com/group/comp....bc47f64cc96cce
>



--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
different results from compiled dll deployment to different server =?Utf-8?B?Ymlj?= ASP .Net 1 05-05-2007 09:14 AM
RE: hash() yields different results for different platforms Kerry, Richard Python 2 07-13-2006 01:16 PM
hash() yields different results for different platforms Qiangning Hong Python 12 07-12-2006 02:08 PM
Different defrag utilities, different results! cygnian@msn.com Computer Support 6 05-12-2005 06:25 PM
Preview image directly on PC, save directly to HD Patrick M. Digital Photography 3 01-07-2004 08:29 PM



Advertisments