Consider this perspective: if each individual is going to be given a name, it becomes simpler to assign the getName
function to its prototype.
In this scenario, you create a Person with a prototype function for obtaining a name. Then, a few lines down, you define a Me function that automatically sets a name. Since we want to include the getName
function, we utilize the Person
which already includes that function object. When you instantiate Me()
and assign it to me
, you can invoke getName()
to retrieve the name, defaulting to "John Resig".
The same outcome can be achieved without prototyping, like so:
function Me(){
this.name = "John Resig";
this.getName = function () {
return this.name;
}
}
var me = new Me();
...HOWEVER, utilizing prototypes streamlines object creation and enhances efficiency. You may also find more information in this explanation.