Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>"Everything is an object"</em>... that's one of the big misconceptions that exist all around the language.</p> <p><strong>Not</strong> <em>everything</em> is an object, there are what we call <em>primitive values</em>, which are string, number, boolean, null, and undefined.</p> <p>That's true, a string is a <strong>primitive value</strong>, but you can access all the methods inherited from <code>String.prototype</code> as if it were an object.</p> <p>The property <a href="http://ecma262-5.com/ELS5_HTML.htm#Section_11.2.1" rel="noreferrer">accessor operators</a> (the dot and the bracket notation), temporarily convert the string value to a String object, for being able to access those methods, e.g.:</p> <pre><code>"ab".charAt(1); // "b" </code></pre> <p>What happens behind the scenes is something like this:</p> <pre><code>new String("ab").charAt(1); // "b", temporal conversion ToObject </code></pre> <p>As with the other primitive values, such as <code>Boolean</code>, and <code>Number</code>, there are <em>object wrappers</em>, which are simply objects that contain the primitive value, as in your example:</p> <pre><code>var strObj = new String(""); strObj.prop = "foo"; typeof strObj; // "object" typeof strObj.prop; // "string" </code></pre> <p>While with a primitive:</p> <pre><code>var strValue = ""; strValue.prop = "foo"; typeof strValue; // "string" typeof strValue.prop; // "undefined" </code></pre> <p>And this happens because again, the property accessor on the second line above, creates a new temporal object, as:</p> <pre><code>var strValue = ""; new String(strValue).prop = "foo"; // a new object which is discarded //... </code></pre>
 

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