function Person(){
this.scream = function(){
alert('NO NO NO!!!!');
};
}
var steve = new Person();
steve.scream() // NO NO NO!!!!
Person.prototype.scream = function(){
alert('YES YES YES!!!!');
}
steve.scream() // still NO NO NO!!!!
Is there a way to override ‘scream‘ without referencing steve explicitly? Think about the cases when you have may instances of Person.
No,
Having that
Persondeclaration, every time you create a new “instance” of it the “constructor” will run and you’ll create a completely newscreamfunction (closure) which you don’t have any reference to, except from the newly created object,steve.screamthat is.As an alternative you may do it like this:
In which case the initial
scream“method” is available only in one place, on the prototype, and you can overwrite it for all “instances”.