How to delete a function from constructor?
If there is a function called greet in the Person constructor, how do I remove the function?
function Person(name)
{
this.name = name;
this.greet = function greet()
{
alert("Hello, " + this.name + ".");
};
}
I want the result to be:
function Person(name)
{
this.name = name;
}
You cannot change the source of a function. If you want to change that function’s behaviour, you have to options:
Override the function with your own. This is easy if the function is standalone. Then you can really just define
after the original function was defined. But if prototypes and inheritance are involved, it can get tricky to get a reference to the original prototype (because of the way how function declarations are evaluated).
Ceate a wrapper function which creates and instance and removes the properties you don’t want:
This approach is also limited since you can only change what is accessible from the outside. In the example you provided it would be sufficient though.