1

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?

Allan Socks
  • 261
  • 2
  • 10
  • 1
    [answered in documentations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Prototype-based_programming) – Denys Séguret Jun 17 '14 at 07:20
  • The tutorial seems to be poorly phrased (or simply wrong). The prototype provides property values that an object will use if it doesn't have its own values overriding those. It's how Javascript does inheritance. – user2357112 Jun 17 '14 at 07:23
  • Thank you. I will try to learn about the prototype property from another tutorial I guess. – Allan Socks Jun 17 '14 at 07:27
  • If you assign a value to an instance member directly its shadowed, this and much more is explained here: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Jun 17 '14 at 12:38

1 Answers1

0

I think you are getting confused with the line :

employee.prototype.salary=null;

..in that you think you are defining a variable called salary. Yes you are defining a variable called salary, but the line :

fred.salary=20000;

..is also defining a variable called salary.

However if you created a few other people-objects :

var fred=new employee("Fred Flintstone","Caveman",1970);
var jane=new employee(...);
var bob=new employee(...);

And then did changed the salary on prototype..

employee.prototype.salary=30000;
fred.salary=20000;

then fred.salary would be 20000, but bob and jane would be 30000

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225