This is a very bad idea, for a great number of reasons. A few of which are:
- Adding methods to the prototype in the constructor will cause the prototype method to be replaced for all instances, everytime you instantiate a new Dog.
- Calling
Dog.prototype.bark()
means thatthis
will beDog.prototype
and not your instance ofDog
, which can cause serious issues. this.bark = function () { Dog.prototype.bark(); }
is some serious WTF. Becausethis.bark
will already evaluate to the prototype method making this unnecessary. And calling it like this actually destroys the naturalthis
value, as mentioned in #2.
Here is what is should be:
function Dog() {
this.makeSound();
};
Dog.prototype.bark = function() {
alert('woof');
};
Dog.prototype.makeSound = function() {
this.bark();
};
Or alternatively, without the prototype at all:
function Dog() {
this.bark = function() {
alert('woof');
};
this.makeSound = function() {
this.bark();
};
this.makeSound();
};
I would not trust this snippet of yours at all.