Quantcast
Channel: Stephen Belanger - Latest Comments
Viewing all articles
Browse latest Browse all 29

Re: A simple explanation of “new” in Javascript.

$
0
0

While we've already touched `instanceof` it might be worth mentioning that it's actually nothing more than a lookup in the prototype chain; basically, `a instanceof b` checks if the object a has b.prototype as its prototype, or if it's the "prototype of the prototype", and so on.

This could lead to subtle problems, as in the following:

    function Test() {}
    var test = new Test()
    test instanceof Test // true
    
    Test.prototype = {foo: 42};
    test instanceof Test // false
    test.foo // undefined (since Test.prototype doesn't refer to the [[Prototype]] of test)


Viewing all articles
Browse latest Browse all 29

Latest Images

Trending Articles





Latest Images