node.js - why I can't "overwrite" a method in javascript? -
i override function of js object adding prototype.
var originalobject = function(){ this.somefun = function(){ // ... } } var otherobj = function(){ this.otherfun = function(){ originalobject.prototype.fun = function(){ return 'this overwritten function.'; } var o = new originalobject(); return o.fun() } }
this way when execute new otherobj().otherfun()
have expected result ('this overwritten function.'), if function i'm trying add part of originalobject
var originalobject = function(){ this.fun = function(){ return 'this original function'; } this.somefun = function(){ // ... } }
then result not expected one, infact new otherobj().otherfun()
returns 'this original function'.
so, this way add method prototype , override it? , important why can't "overwrite" function in prototype?
in js objects there 2 levels depend on directly object , depend on directly prototype. , when call property in object, starts search root looks in branches. can see tree belowed picture:
Comments
Post a Comment