PDA

View Full Version : Trying to instantiate an object...


jakbo
10-08-2005, 08:22 PM
I'm not sure exactly how to do this. I have a file called datafile.js. I have another file called Objects.js. I have another file called sales.html to bring it alll together.

Here is datafile.js, which I created to test my other stuff:
//datafile.js
var datafile = new Array();
datafile.push("Smith,Mary:9/9/2001:sw:5000:24000");
datafile.push("Wilen, Bill:4/18/1994:ne:4000:26000");
datafile.push("Wang, Chon:7/1/1998:ne:6000:30000");
datafile.push("Normal, Abi:1/21/2000:nw:10000:25000");
datafile.push("Arnold, Sarah:10/16/1990:sw:2000:27000");
datafile.push("Lowe, Sam:1/13/1992:sw:2000:24500");
datafile.push("Brody, Louise:1/26/1998:ne:3000:28000");
datafile.push("Chee, James:4/15/2004:sw:3200:25000");
datafile.push("Webb, Jack:11/4/2002:nw:6500:32500");
datafile.push("Mann, Beth:11/4/2006:nw:6500:32500");
datafile.push(":11/4/2002:nw:6500:32500");
datafile.push("Nabo, Ned:8/4/2003:nw:7100:-30900");
datafile.push("Inderfurth, Apo:2/4/2000:xw:3100:9900");


Here is Objects.js, which I'm using for all of my functions and to sort through the data:

//Objects.js
function verify(Employee){
alert("Just added "+Employee.name+".");
}

function Employee(name, dateOfHire, division, sales, salary){
this.name=name;
this.dateOfHire=dateOfHire;
this.division=division;
this.sales=sales;
this.salary=salary;
this.show=showAll;
verify(this);
this.empNo=++Employee.key;
}
Employee.key=0;


function showAll(){
document.writeln(this.name+"<br>");
document.writeln(this.dateOfHire+"<br>");
document.writeln(this.division+"<br>");
document.writeln(this.sales+"<br>");
document.writeln(this.salary+"<br>");
}


var workers=new Array();
function stepThru(){
for(i=0;i<datafile.length;i++){
workers[i]=new Array();
workersAttr=datafile[i].split(':');
for (j=0;j<workersAttr.length;j++){
workers[i][j]=workersAttr[j];
}
}
}



And this is what I'm using to bring it all together:
//sales.html
<html>
<head>
<style type = "text/css">
table{font-family: verdana,arial,sans-serif;
font-size: .7em;}
</style>
<script src = "datafile.js" type = "text/javascript"></script>
<script src = "Objects.js" type = "text/javascript"></script>
<script type = "text/javascript">
</script>
</head>
<body onload="stepThru();">
<table>
</table>
</body>
</html>

I can recreate the results from stepThru() manually to instantiate a new Employee, but I can't figure out how to do it automatically each time a new datafile is added to datafile.js.

This is what I did when I was trying to see if my object would work in the first place:
var workers = new Array();
workers[workers.length] = new Employee("Smith, Mary","9/9/2001","sw",5000,24000);

Would somebody explain to me how to instantiate the Employee object straight from the stepThru function?

rm-f
10-09-2005, 01:13 AM
var workers=new Array();
function stepThru(){
for(i=0;i<datafile.length;i++){
// extract name, dateOfHire... here (from datafile[i])
workers[i] = new Employee(name, dateOfHire...)
}
}

jakbo
10-09-2005, 08:32 AM
Thanks for the reply. Are you saying I should forget about splitting the records up right there and split them up somewhere else?

jakbo
10-09-2005, 08:58 PM
Anybody have any ideas about this?

rm-f
10-11-2005, 02:25 PM
Your questions was:Would somebody explain to me how to instantiate the Employee object straight from the stepThru function?
You can extract data at any place, prior to creating of a new instance of Employee.

It is possible you want to do something else hat is not clearly understandible. If this is the case try to rephrase the question.

jakbo
10-11-2005, 10:48 PM
Oh, sorry about that and thanks for replying, I've been having trouble with this for quite a while.

The datafile which I've been given to work from is an array of records and all of the attributes are separated by a colon. I wanted to step through the records and split each attribute on the colons. Then I created an array of arrays out of it so that I can access them like this: array[1][0] where the first index is the full record and the second index is a particular attribute of that record. I can't figure out how to get each one of these into a function constructor I made. I'd like to do it something like this:

var workers=new Array();
function stepThru(){
for(i=0;i<datafile.length;i++){
workers[i]=new Array();
workersAttr=datafile[i].split(':');
for (j=0;j<workersAttr.length;j++){
workers[i][j]=workersAttr[j];
/*I'd like to instantiate or create an instance of the object right here, but everything I've tried so far hasn't worked. If I can get it into the constructor from right here, then I can start working with it from the object and start adding my methods to it. Do I need to add another for loop for that?*/
}
}

Again, thanks for the help and for your patience. I appreciate it.

rm-f
10-13-2005, 07:25 PM
<html>
<head>
<script>

//datafile.js
var datafile = new Array();
datafile.push("Smith,Mary:9/9/2001:sw:5000:24000");
datafile.push("Wilen, Bill:4/18/1994:ne:4000:26000");
datafile.push("Wang, Chon:7/1/1998:ne:6000:30000");
datafile.push("Normal, Abi:1/21/2000:nw:10000:25000");
datafile.push("Arnold, Sarah:10/16/1990:sw:2000:27000");
datafile.push("Lowe, Sam:1/13/1992:sw:2000:24500");
datafile.push("Brody, Louise:1/26/1998:ne:3000:28000");
datafile.push("Chee, James:4/15/2004:sw:3200:25000");
datafile.push("Webb, Jack:11/4/2002:nw:6500:32500");
datafile.push("Mann, Beth:11/4/2006:nw:6500:32500");
datafile.push(":11/4/2002:nw:6500:32500");
datafile.push("Nabo, Ned:8/4/2003:nw:7100:-30900");
datafile.push("Inderfurth, Apo:2/4/2000:xw:3100:9900");

//Objects.js
function verify(Employee){
alert("Just added "+Employee.name+".");
}

function Employee(name, dateOfHire, division, sales, salary) {
this.name=name;
this.dateOfHire=dateOfHire;
this.division=division;
this.sales=sales;
this.salary=salary;
this.show=showAll;
//verify(this);
this.empNo = ++Employee.key;
}
Employee.key=0;


function showAll(){
document.write(this.name+"<br>");
document.write(this.dateOfHire+"<br>");
document.write(this.division+"<br>");
document.write(this.sales+"<br>");
document.write(this.salary+"<br>");
}

// workers is array of Employees
var workers=new Array();

function stepThru() {
for(var i=0; i<datafile.length; i++) {
workersAttr = datafile[i].split(':');
workers[i] = new Employee(workersAttr[0],workersAttr[1],workersAttr[2],workersAttr[3],workersAttr[4]);
}
}




</script>

<body>


<script>

stepThru(); // load values


document.write('<table border="1">');
for(var i=0; i<workers.length;i++) {
document.write('<tr>');
document.write('<td>' + workers[i].name + '</td>');
document.write('<td>' + workers[i].dateOfHire + '</td>');
document.write('<td>' + workers[i].division + '</td>');
document.write('<td>' + workers[i].sales + '</td>');
document.write('<td>' + workers[i].salary + '</td>');
document.write('<td>' + workers[i].empNo + '</td>');
document.write('</tr>');
}
document.write('</table>');

document.write('<br><br><br>');


for(var i=0; i<workers.length; i++) {
workers[i].show();
document.write('<hr>');
}

</script>



</body>
</html>