[ Web Proxy ]
URL:
Viewing: https://javascript.info/task/strange-instanceof [Back]  [Original]

Strange instanceof
EN

We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

    Search on Javascript.info:
    Search in the tutorial:
    Light themeDark theme
    DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
    back to the lesson

    Strange instanceof

    importance: 5

    In the code below, why does instanceof return true? We can easily see that a is not created by B().

    function A() {}
    function B() {}
    
    A.prototype = B.prototype = {};
    
    let a = new A();
    
    alert( a instanceof B ); // true
    solution

    Yeah, looks strange indeed.

    But instanceof does not care about the function, but rather about its prototype, that it matches against the prototype chain.

    And here a.__proto__ == B.prototype, so instanceof returns true.

    So, by the logic of instanceof, the prototype actually defines the type, not the constructor function.


    Web Proxy Viewer  |  New URL  |  Original Page