0

Code snippet below gives different output for two instances of same constructor (Child). First object(instance1) is without prototype and second(instance2) is with prototype, why?

   parent=function(){   
        parent.prototype.method1= function() {}
        parent.prototype.property = true;
    };

    child=function() {
        parent.call(this);    
        child.prototype = new parent(); 
    };

    (function(){
        var instance1 = new child();
        console.log( instance1 );  // Empty Object

        var instance2 = new child();
        console.log( instance2 ); // Object is not empty

    }());

Output:

enter image description here

JSFiddle Link

JS-JMS-WEB
  • 2,555
  • 3
  • 17
  • 26
  • 1
    Because you must not overwrite `.prototype` inside of the constructor! The instance has already been created and the prototype chain has been set up in there. – Bergi Jun 28 '15 at 14:03
  • 1
    Btw, you [should not use `new parent` to create prototypes](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here) – Bergi Jun 28 '15 at 14:04

0 Answers0