I'm trying to figure out what the value is in using the prototype property. One tutorial I've been looking at defines the prototype property as follows: "The prototype property allows you to add properties and methods to an object." So I imagined, given this definition, that if I don't use this I won't be allowed to add properties and methods to an object..the site used this as an example:
function employee(name,jobtitle,born)
{
this.name=name;
this.jobtitle=jobtitle;
this.born=born;
}
var fred=new employee("Fred Flintstone","Caveman",1970);
employee.prototype.salary=null;
fred.salary=20000;
document.write(fred.salary);
So I ran this in my browser and yes, fred.salary does indeed equal 20000. I decided to experiment and see what happened if I deleted this line:
employee.prototype.salary=null;
I thought I would get some error. I imagined that this was probably necessary, given the definition that this "allows" me to "add" properties and methods to an object. I tried this:
function employee(name,jobtitle,born)
{
this.name=name;
this.jobtitle=jobtitle;
this.born=born;
}
var fred=new employee("Fred Flintstone","Caveman",1970);
fred.salary=20000;
document.write(fred.salary);
But, to my surprise there was no error. The code worked exactly the same as if I had made no change at all. It was as if this line:
employee.prototype.salary=null;
was totally meaningless. Now I'm left scratching my head wondering...Why would I use the prototype property if it doesn't appear to do anything?