Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're right that JavaScript doesn't have classes (yet), but it does have constructor functions, an <code>instanceof</code> operator that defines a relationship between objects and constructors, and a form of inheritance based on prototype chains.</p> <p><code>obj instanceof ctor</code> is true when <code>ctor.prototype</code> is on <code>obj</code>'s prototype chain.</p> <p>Modulo the caveat below, you could implement <code>instanceof</code> in EcmaScript 5 thus</p> <pre><code>function isInstanceOf(obj, ctor) { var proto = ctor.prototype; if (typeof obj === "object" || typeof obj === "function") { while (obj) { if (obj === proto) { return true; } obj = Object.getPrototypeOf(obj); } } return false; } </code></pre> <p>Unless you go around reassigning prototypes (<code>o = new MyConstructor(); MyConstructor.prototype = somethingElse</code>) it should be the case that <code>new MyConstructor() instanceof MyConstructor</code>.</p> <p>Section <a href="http://es5.github.com/#x15.3.5.3">15.3.5.3</a> explains this in detail.</p> <blockquote> <p>15.3.5.3 <code>[[HasInstance]] (V)</code></p> <p>Assume F is a Function object.</p> <p>When the [[HasInstance]] internal method of F is called with value V, the following steps are taken:</p> <ol> <li>If V is not an object, return false.</li> <li>Let O be the result of calling the [[Get]] internal method of F with property name "prototype".</li> <li>If Type(O) is not Object, throw a TypeError exception.</li> <li><p>Repeat</p> <ol> <li>Let V be the value of the [[Prototype]] internal property of V.</li> <li>If V is null, return false.</li> <li>If O and V refer to the same object, return true.</li> </ol></li> </ol> </blockquote> <p>This isn't the whole story because host objects (like DOM nodes) are allowed to implement the <code>[[HasInstance]]</code> internal method however they like but most browsers implement host objects to behave as closely to native objects as possible.</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.
 

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