Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>call</code> method sets the <code>this</code> value of the invoked function to the object passed as first argument, in your example, you are executing the <code>Object.prototype.toString</code> method on an Array object.</p> <p>Array objects, have their own <code>toString</code> method (<code>Array.prototype.toString</code>) that shadows the one from <code>Object.prototype</code>, if you call <code>[].toString();</code> the method on the <code>Array.prototype</code> will be invoked.</p> <p>For example:</p> <pre><code>function test() { alert(this); } test.call("Hello"); // alerts "Hello" </code></pre> <p>Another example:</p> <pre><code>var alice = { firstName: 'Alice', lastName: 'Foo', getName: function () { return this.firstName + ' ' + this.lastName; } }; var bob = { firstName: 'Bob', lastName: 'Bar', }; alice.getName.call(bob); // "Bob Bar" </code></pre> <p>In the above example, we use the Alice's <code>getName</code> method on the Bob's object, the <code>this</code> value points to <code>bob</code>, so the method works just as if it were defined on the second object.</p> <p>Now let's talk about the <code>Object.prototype.toString</code> method. All native objects in JavaScript contain an <em>internal property</em> called <code>[[Class]]</code> this property contains a string value that represents the specification defined <em>classification</em> of an object, the possible values for <em>native</em> objects are:</p> <ul> <li><code>"Object"</code></li> <li><code>"Array"</code></li> <li><code>"Function"</code></li> <li><code>"Date"</code></li> <li><code>"RegExp"</code></li> <li><code>"String"</code></li> <li><code>"Number"</code></li> <li><code>"Boolean"</code></li> <li><code>"Error"</code> for error objects such as instances of <code>ReferenceError</code>, <code>TypeError</code>, <code>SyntaxError</code>, <code>Error</code>, etc</li> <li><code>"Math"</code> for the global <code>Math</code> object</li> <li><code>"JSON"</code> for the global JSON object defined on the ECMAScript 5th Ed. spec.</li> <li><code>"Arguments"</code> for the <code>arguments</code> object (also introduced on the ES5 spec.)</li> <li><code>"null"</code> (introduced just a couple of days ago in the <a href="http://wiki.ecmascript.org/lib/exe/fetch.php?id=es3.1%3Aes3.1_proposal_working_draft&amp;cache=cache&amp;media=resources:es5_errata_7-1-10.pdf">ES5 errata</a>)</li> <li><code>"undefined"</code></li> </ul> <p>As I've said before that property is <em>internal</em>, there is no way to change it, the specification doesn't provide any operator or built-in function to do it, and the <em>only</em> way you can access its value is through the <code>Object.prototype.toString</code> method.</p> <p>This method returns a string formed by:</p> <pre><code>"[object " + this.[[Class]] + "]" </code></pre> <p><sub>Only for expository purposes because <code>[[Class]]</code> cannot be accessed directly.</sub></p> <p>For example:</p> <pre><code>Object.prototype.toString.call([]); // "[object Array]" Object.prototype.toString.call(/foo/); // "[object RegExp]" Object.prototype.toString.call({}); // "[object Object]" Object.prototype.toString.call(new Date); // "[object Date]" // etc... </code></pre> <p>This is really useful to detect the <em>kind</em> of an object in a safe way, for detecting array objects, it's the most widely used technique:</p> <pre><code>function isArray(obj) { return Object.prototype.toString.call(obj) == '[object Array]'; } </code></pre> <p>It might be tempting to use the <code>instanceof</code> operator, but that way will lead to problems if you work on <em>cross-frame</em> environments, because an array object created on one frame, will not be <code>instanceof</code> the <code>Array</code> constructor of another.</p> <p>The above method will work without any problems, because the object will contain the value of its <code>[[Class]]</code> internal property intact.</p> <p>See also: </p> <ul> <li><a href="http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/"><code>instanceof</code> considered harmful (or how to write a robust <code>isArray</code>)</a></li> <li><a href="http://ecma262-5.com/ELS5_Section_15.htm#Section_15.2.4.2"><code>Object.prototype.toString</code></a></li> <li><a href="http://ecma262-5.com/ELS5_Section_8.htm#Section_8.6.2">Object Internal Properties and Methods</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload