Well, the basics first: when you write function Person(o) { ... } , you are not declaring a class - JavaScript is not based on a class, but based on an object. This operator simply declares a function (which, incidentally, is also an object).
Next, when you create such an object:
var mellon = new Person('Mellon');
you create an object whose constructor (of its kind) is Person .
Now read this carefully: since the constructor is a mellon Person , all objects in the Person prototype will be available in the object.
So if you write:
Person.prototype.shower = function(){ this.dirtFactor=2 }
then the mellon.shower() method will be available.
I recommend reading Mozilla's introduction to OOP in Javascript for more information on this topic.
So, to answer your question: this refers to the object with which the shower method was called. In the above case it will be mellon .
jrharshath
source share