posted 8/3/2011 by Raghav Khunger
I recently encountered one question on forums "Prototype is not overriding the function/method in JavaScript" by a person. He was having the below code:
function Test() { this.foo = function() { console.log('base foo'); }; } Test.prototype.foo = function() { console.log('overridden foo'); }; var test = new Test(); test.foo();
And running the above code the output was coming "base foo" as contrast to "overridden foo" which seems to be the output by looking the code simply. But actually the output was coming correct as it supposed to behave. In the above code "Test" class is a plain function whose prototype is an empty Object i.e. {}. Writing this code:
Test.prototype.foo = function() { console.log('overridden foo'); };
this.foo = function() { console.log('base foo'); };
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18