I am wondering can we still change the function body once it is constructed ?
var O = function(someValue){
this.hello = function(){
return "hello, " + someValue;
}
}
O.prototype.hello = function(){
return "hhhhhhh";
}
var i = new O("chris");
i.hello(); // -> this still returns the old definition "hello, chris"
The javascript statement O.prototype.hello = function(){....}
doesn't override and redefine the hello function behavior. Why is that ? I know it will have a type error if you tried to reuse the parameter someValue
.
// this will fail since it can't find the parameter 'someValue'
O.prototype.hello = function(){
return "aloha, " + someValue;
}
I am wondering why It allows to add function during runtime like
O.prototype.newFunction = function(){
return "this is a new function";
}
i.newFunction(); // print 'this is a new function' with no problem.
but doesn't allow you to change the definition once it's defined.
Did i do something wrong ? how do we override and redefine a function within a class ? and is there a way to reuse the parameter that we passed in earlier to create the object ? in this cases how do we re-use someValue
if we want to extend more functions to it.
When you access a property, system first looks for it in the instance. If it is not found, it looks for it in the prototype. This is why this.hello is being used, rather than O.prototype.hello.
If you wish to override the implementation of hello, you will need to use JavaScript inheritance. Here is a basic example:
This is because when you access a property of an Object, JavaScript check the properties of the objects first, before going into its prototype.
This is analogous to Java's derived class overriding the base class functionality.
For a better understanding check the code example in Inheriting properties
Also note that someValue in your case is local to the constructor function. If you need it in other functions, you should assign it to this.someValue inside the constructor.
You'll be able to override the hello function for a particular Object here like the following. But not for the entire Class.
i.hello = function(){ console.log('even here someValue is not accessible');};
Note that here we have not changed the constructor, and hence this will not work with other instances like
test
in the above example.The best way to do it would be to define functions only in the prototype & not in the constructor, like the following snippet.