I defined my classes like that:
function Employee () {
this.name = "";
this.val = new Array();
}
function WorkerBee () {
this.beeQueen = "lola";
this.setVal = function(val) {
this.val.push(val);
};
}
WorkerBee.prototype = new Employee;
function SalesPerson () {
this.dept = "development";
}
SalesPerson.prototype = new WorkerBee;
function Engineer () {
this.dept = "R&D";
}
Engineer.prototype = new WorkerBee;
Problem: all the objects I create share the same val array:
var mark = new WorkerBee;
mark.name = "Mark";
mark.setVal('00000')
var louis = new SalesPerson;
louis.name = "Louis";
louis.setVal('11111')
var ricky = new SalesPerson;
ricky.name = "Ricky";
ricky.setVal('33333')
var john = new Engineer;
john.name = "John";
john.setVal('55555');
This:
html += "<br /><br />Name: " + mark.name;
html += "<br />Val: " + mark.val;
html += "<br /><br />Name: " + louis.name;
html += "<br />Val: " + louis.val;
html += "<br /><br />Name: " + ricky.name;
html += "<br />Val: " + ricky.val;
html += "<br /><br />Name: " + john.name;
html += "<br />Val: " + john.val;
displays:
Name: Mark
Val: 00000,11111,33333,55555
Name: Louis
Val: 00000,11111,33333,55555
Name: Ricky
Val: 00000,11111,33333,55555
Name: John
Val: 00000,11111,33333,55555
I read http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ and http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/ but I'm still confused!
When I use a string instead of an array this works well (because the reference to the string is overwritten I suppose) but how to do it with array ?
So I can have:
Name: Mark Val: 00000
Name: Louis Val: 11111
Name: Ricky Val: 33333
Name: John Val: 55555