Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>String literals are primitives (<a href="http://bclary.com/2004/11/07/#a-4.3.16" rel="nofollow noreferrer">String values</a>), <a href="http://bclary.com/2004/11/07/#a-4.3.18" rel="nofollow noreferrer">String objects</a> can be created with the <a href="http://bclary.com/2004/11/07/#a-15.5.2.1" rel="nofollow noreferrer">String constructor</a> in a <code>new</code> expression:</p> <pre><code>"foo" instanceof String // false new String("foo") instanceof String // true </code></pre> <p><strong>Edit:</strong> Something that seems to be confusing (by looking at the accepted answer <a href="https://stackoverflow.com/questions/203739/why-does-instanceof-return-false-for-some-literals">here</a>), is that you can still access properties defined on the <em>prototype objects</em> of primitive values, for example:</p> <pre><code>"foo".indexOf == String.prototype.indexOf // true "foo".match == String.prototype.match // true String.prototype.test = true; "foo".test // true true.toString == Boolean.prototype.toString (3).toFixed == Number.prototype.toFixed // true // etc... </code></pre> <p>The reason of that relies on the <a href="http://bclary.com/2004/11/07/#a-11.2.1" rel="nofollow noreferrer">Property Accessors</a>, the dot notation <code>.</code> and the bracket notation <code>[]</code>.</p> <p>Let's give a look to the algorithm in the ECMA-262 specification:</p> <p>The production MemberExpression : MemberExpression [ Expression ] (or MemberExpression . Identifier) is evaluated as follows:</p> <ol> <li><p>Evaluate <code>MemberExpression</code>.</p></li> <li><p>Call <code>GetValue(Result(1))</code>.</p></li> <li><p>Evaluate Expression.</p></li> <li><p>Call <code>GetValue(Result(3))</code>.</p></li> <li><p>Call <code>ToObject(Result(2))</code>.</p></li> <li><p>Call <code>ToString(Result(4))</code>.</p></li> <li><p>Return a value of type Reference whose base object is Result(5) and whose property name is <code>Result(6)</code>.</p></li> </ol> <p>In the <strong>Step 5</strong>, the <a href="http://bclary.com/2004/11/07/#a-9.9" rel="nofollow noreferrer"><code>ToObject</code></a> internal operator type-converts the MemberExpression to object, depending on it's type.</p> <p>The primitives are converted to Objects without noticing, and that's why you can access the properties defined on the prototype.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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