1

I am learning prototype in JavaScript and this is the code I am trying -

<script>
function employee(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
}

var trialcoder = new employee('trialcoder', 26, 'M');
//employee.prototype.salary = null;
trialcoder.salary = 19000;

document.write("salary is "+ trialcoder.salary);
</script>

My thoughts- To add another property we need to use prototype like - employee.prototype.salary = null; so on un commenting this line, I was expecting an error but it was not..let me know where I am wrong in the prototype concept.

Code Source - http://www.w3schools.com/jsref/jsref_prototype_math.asp

Trialcoder
  • 5,816
  • 9
  • 44
  • 66
  • Read http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ – KV Prajapati Jun 24 '13 at 05:24
  • might be a dupe of http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work –  Jun 24 '13 at 05:24
  • 2
    please refer Mozilla Developer Network (MDN) henceforth, w3schools is a bit *iffy* with their concepts and explanations – painotpi Jun 24 '13 at 05:29
  • 1
    @badZoke http://www.w3fools.com/ – SheetJS Jun 24 '13 at 05:30
  • not an answer to your question, but don't use `w3schools` as reference or for learning, they used to have (and i'm sure it is sill so, because of the this sample) many false informations and misleading informations/examples. – t.niese Jun 24 '13 at 05:30
  • 1
    @Trialcoder Why were you expecting an error? – painotpi Jun 24 '13 at 05:31
  • @badZoke I was expecting error on commenting the line `employee.prototype.salary = null;` as salary property is not mentioned in the employee function – Trialcoder Jun 24 '13 at 05:33
  • 1
    @Trialcoder you can always set a property of an object otherwise your `this.name = name` would also not work. Using `employee.prototype.salary = null` set's the default value of `salary` for all `employee` objects to `null` which otherwise is `undefined`. – t.niese Jun 24 '13 at 05:40

2 Answers2

5

Your code is correct, because when you called

var trialcoder = new employee('trialcoder', 26, 'M');

You got an object instance of employee and just like any other object you can add properties to your trialcoder object like

trialcoder.salary = 19000;

In this case, the salary property is only available to your trialcoder object and if you make another instance of employee like var another = new employee() you have no salary property in another object, but, if you do something like

function employee(name, age, sex) { //... }
employee.prototype.salary = 19000;

and then make instances like

var anEmp = new employee();
console.log(anEmp.salary); // 19000

Make another instance

var newEmp = new employee();
console.log(newEmp.salary); // 19000

if you want, you can

newEmp.salary = 10000;
console.log(anEmp.salary); // 10000

Which means, when you add a property in the prototype of a constructor (employee) then every object instance can share the same property and after making an instance from the constructor, you can change the property of an instance but this won't effect other instances. Hope it's clear enough now.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

Your code is right and you will not receive error because using prototype your setting property salary of class employee and after creating an object of your class ur are setting the property for that specific object,if you create another object you can set its property salary too If you set property using prototype then all objects of that class will share that (salary) property .

EduardoSaverin
  • 545
  • 4
  • 19