I've been trying to understand the concept of prototypes in JS, but for some reason I'm finding it really baffling. Why and when would you use a prototype?
What's the difference between this (from this MDN example):
function Person(gender) {
this.gender = gender;
}
Person.prototype.sayHello = function()
{
alert ('hello');
};
And this:
function Person(gender) {
this.gender = gender;
this.sayHello = function() {
alert('hello');
};
}
I think I understand how to use them, but I don't know why I would use them. Maybe I'm missing something - scratch that - CLEARLY I'm missing something!
Can someone please explain the difference between those two examples and why I should use one over the other?
Thanks!