Why can't I set a JavaScript prototype inside a function? -
why can't set prototype within of function? illustration why not work?
var bar = function(){ this.name='bar' } var barproto = new bar() var foo = function(){ this.prototype= barproto } var foo = new foo() console.log(foo.name) // undefined but work:
var bar = function(){ this.name='bar' } var barproto = new bar() var foo = function(){ } foo.prototype= barproto var foo = new foo() console.log(foo.name) // bar i don't syntax of assigning prototype after have created function.
this.prototype= barproto
is not equivalent
foo.prototype= barproto this refers specific object created new foo()
foo constructor function. set prototype on constructor, not on specific instance.
more info on prototypical inheritance here: mozilla docs
javascript prototype
No comments:
Post a Comment