使用 Javascript 原型继承将数据存储在数组中
Storing data in an array using Javascript Prototypical inheritance
做一些 javascript 原型继承,我想将参数推送到我的 Grades constructor
中并进行存储操作,并使用我的存储方法将数据推送到我的 this.students
数组中,然后在我的其他方法中随意使用这些值。
但问题是,当我控制台记录构造函数时,它会按照我的需要将数据推送到 this.students
数组中,但每个对象都未定义。
这很奇怪,因为如果我 运行 在 Grades constructor
中使用 for 循环,它将完美运行。但我想有一个单独的方法来做到这一点,在我的 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();
}
/*
* 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);
你的问题出在你的存储函数内部,源于定义。
this.studentGrades
实际上定义的是数组的长度,而不是数组本身。
如果您不存储输入数组或将其通过 init(inputGrades)
传递到 storage(inputGrades)
,则您无法从存储原型访问原始输入。
更好:将构造函数位更改为:
this.students = [];
this.studentGrades = studentGrades;
并将您的函数存储到:
for(var i=0; i < this.studentGrades.length; i++) {
this.students.push(this.studentGrades[i]);
}
我想你应该没问题。
更新:您的原始函数调用具有可变数量的参数。
获得完整答案的最简单方法是将参数变量更改为:
var inputGrades = new Grades( [[89,78,93,78], [83,67,93,98], [93,99,73,88]]);
现在你只发送一个参数,一组数组。
备选方案:将函数更改为
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;
然后你应该可以发送多个参数。
我认为您的代码存在一些问题,尤其是 Grades 构造函数:
function Grades(studentGrades) {
if(!Array.isArray(studentGrades)) return true;
this.students = [];
this.studentGrades = arguments.length;
this.numRows = 0;
this.numColumns = 0;
this.init();
}
您正在使用数组作为函数的参数,但您传递的是三棵树参数(数组),我认为这一行:
var inputGrades = new Grades( [89,78,93,78], [83,67,93,98], [93,99,73,88] );
应该是这样的:
var inputGrades = new Grades( [[89,78,93,78], [83,67,93,98], [93,99,73,88] ]);
下面的行 this.studentGrades = arguments.length;
在构造函数中没有用,可能会导致您的代码出现问题,应替换为:
this.studentGrades = arguments;
或者如果你像我一样传递一个数组数组,你可以使用:
this.studentGrades = studentGrades;
做一些 javascript 原型继承,我想将参数推送到我的 Grades constructor
中并进行存储操作,并使用我的存储方法将数据推送到我的 this.students
数组中,然后在我的其他方法中随意使用这些值。
但问题是,当我控制台记录构造函数时,它会按照我的需要将数据推送到 this.students
数组中,但每个对象都未定义。
这很奇怪,因为如果我 运行 在 Grades constructor
中使用 for 循环,它将完美运行。但我想有一个单独的方法来做到这一点,在我的 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();
}
/*
* 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);
你的问题出在你的存储函数内部,源于定义。
this.studentGrades
实际上定义的是数组的长度,而不是数组本身。
如果您不存储输入数组或将其通过 init(inputGrades)
传递到 storage(inputGrades)
,则您无法从存储原型访问原始输入。
更好:将构造函数位更改为:
this.students = [];
this.studentGrades = studentGrades;
并将您的函数存储到:
for(var i=0; i < this.studentGrades.length; i++) {
this.students.push(this.studentGrades[i]);
}
我想你应该没问题。
更新:您的原始函数调用具有可变数量的参数。 获得完整答案的最简单方法是将参数变量更改为:
var inputGrades = new Grades( [[89,78,93,78], [83,67,93,98], [93,99,73,88]]);
现在你只发送一个参数,一组数组。
备选方案:将函数更改为
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;
然后你应该可以发送多个参数。
我认为您的代码存在一些问题,尤其是 Grades 构造函数:
function Grades(studentGrades) {
if(!Array.isArray(studentGrades)) return true;
this.students = [];
this.studentGrades = arguments.length;
this.numRows = 0;
this.numColumns = 0;
this.init();
}
您正在使用数组作为函数的参数,但您传递的是三棵树参数(数组),我认为这一行:
var inputGrades = new Grades( [89,78,93,78], [83,67,93,98], [93,99,73,88] );
应该是这样的:
var inputGrades = new Grades( [[89,78,93,78], [83,67,93,98], [93,99,73,88] ]);
下面的行 this.studentGrades = arguments.length;
在构造函数中没有用,可能会导致您的代码出现问题,应替换为:
this.studentGrades = arguments;
或者如果你像我一样传递一个数组数组,你可以使用:
this.studentGrades = studentGrades;