简体   繁体   中英

Storing data in an array using Javascript Prototypical inheritance

Doing some javascript prototypical inheritance, I would like to push the arguments in my Grades constructor and do the storage manipulation and push the data inside my this.students array using my storage method, and then use the values as I please within my other methods.

But the problem is that when I console log the constructor, it does what I need it to in terms of pushing the data in the this.students array but each object comes up as undefined.

This is weird because if I run the for loop inside the Grades constructor it will work perfectly. But I would like to have a separate method to do this, inside of within my Grades constructor

Any help in pointing me in the right direction would be great! Thanks!

function Grades(studentGrades) {

    if(!Array.isArray(studentGrades)) return true;

    this.students = [];
    this.studentGrades = arguments.length;
    this.numRows = 0;
    this.numColumns = 0;

    this.init();
}

/*
* Method to initialize functions
*/
Grades.prototype.init = function() {
    this.storage();
};

/*
* Method to store a list of grades in an array object
*/
Grades.prototype.storage = function() {
    for(var i=0; i < this.studentGrades; i++) {
        this.students.push(this.studentGrades[i]);
    }
};

/*
* Method to add grades
*/
Grades.prototype.addGrades = function(numRows, numColumns, initial) {
    for(this.numRows; this.numRows < this.students.length; this.numRows++ ) {

    }
};

/*
* Method to display the students average
*/
Grades.prototype.display = function() {
    // body...
};


var inputGrades = new Grades( [89,78,93,78], [83,67,93,98], [93,99,73,88] );


console.log(inputGrades);

Your problem is inside your storage function, originating from definition.

this.studentGrades is actually defined as the length of the array, not the array itself.

If you do not store the input array or pass it on through init(inputGrades) to storage(inputGrades) , then you cannot access the original input from your storage prototype.

Better: change constructor bit to:

this.students = [];
this.studentGrades = studentGrades;

And your function inside storage to:

for(var i=0; i < this.studentGrades.length; i++) {
    this.students.push(this.studentGrades[i]);
}

And you should be fine I think.

UPDATE: your original function call has a variable number of arguments. Simplest way to get to complete answer is to change argument variable to:

var inputGrades = new Grades( [[89,78,93,78], [83,67,93,98], [93,99,73,88]]);

Now you send only one argument, an array of arrays.

Alternative: change the function to

function Grades() { // so no input argument

 if(!Array.isArray(studentGrades)) return true;

  this.students = [];
  this.studentGrades = Array.prototype.slice.call(arguments);
  this.numRows = 0;
  this.numColumns = 0;

And then you should be able to send in multiple arguments.

I think there are some problems with your code, especially with Grades constructor :

function Grades(studentGrades) {

    if(!Array.isArray(studentGrades)) return true;

    this.students = [];
    this.studentGrades = arguments.length;
    this.numRows = 0;
    this.numColumns = 0;

    this.init();
}

You are using an array as parameter to the function but you are passing thtree parameters (arrays), I think this line:

var inputGrades = new Grades( [89,78,93,78], [83,67,93,98], [93,99,73,88] );

Should be like this:

var inputGrades = new Grades( [[89,78,93,78], [83,67,93,98], [93,99,73,88] ]);

And the following line this.studentGrades = arguments.length; is useless in the constructor and may cause problems in your code, and should be replaced with :

this.studentGrades = arguments;

Or if you pass an array of arrays like I did you can use:

this.studentGrades = studentGrades;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM