javascript - Why Object#create must precede instantiation of child class -


i have simple inheritance issue in javascript child class inherits parent class. however, in using object#create establish inheritance, seems must come before instantiation of child class:

function child() {}  function parent() {}  parent.prototype.sayhello = function () {     console.log("hello world"); }  // apparently, must precede instantiation: // child.prototype = object.create(parent.prototype); //works, console logs "hello world"  var c = new child();  // typeerror: c.sayhello not function child.prototype = object.create(parent.prototype);  c.sayhello(); //typeerror: c.sayhello not function 

my thinking if child.prototype = object.create(parent.prototype); came after instantiation, still setting child class's prototype point parent's prototype, when sayhello called, prototypal chain searched until method found. why doesn't work? must misunderstanding fundamental.

when write new child(), returned object linked (via __proto__) current value of child.prototype.

when assign new object child.prototype after that, not affect existing instance (which still points old prototype object).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -