您正在构建学生的数组。
var stud1 = [...]
创造的Students
数组我想像它会更最好的Students
功能重命名为Student
(因为它只有一个构建学生)和重命名你的数组stud1
到studentArray
使事情变得更容易理解。
然后,如果你想调用test()
所有的学生数组中,使用方法:
for(var i=0; i<studentArray.length; i++){
studentArray[i].test();
}
在其他作品中,您要使用的方法test()
阵列上和数组没有那个功能,学生呢。
编辑:
考虑您的意见,您能再拍对象,可以让学生建立跟踪,并作为一个工厂新生。这封装了学生创建逻辑,并将所有学生列在学生建构逻辑之外,并让您更深入地了解JavaScript对象的构建。
此外,让你的学生负责了解他们的平均GPA,因为你已经让他们跟踪他们的个人GPA。因此,添加一个功能getAverageGPA()
,然后每当你想打印学生时,请拨打print()
函数打印出他们的所有信息并获得他们的平均GPA。
既然您想在每次创建新学生时都这样做,请让学生工厂(School
)为您做到这一点。
学生: 学生课程包含关于学生的所有信息以及打印他/她的功能以及获得他们的平均GPA。
/**
* Student Object
* @param {String} name
* @param {String} street
* @param {String} city
* @param {String} state
* @param {Array} gpa
* @returns {Student}
*/
var Student = function(name, street, city, state, gpa) {
this.name = name;
this.address = {
street: street,
city: city,
state: state
};
this.gpa = gpa;
};
Student.prototype = {
/**
* Function to print the student and their average GPA
* @returns {undefined}
*/
print: function() {
console.log("Name: " + this.name);
console.log("Address: " +
this.address.street + " " +
this.address.city + " " +
this.address.state);
console.log("Average GPA: " + this.getAverageGPA());
},
/**
* Function to find a students average GPA
* @returns {Number}
*/
getAverageGPA: function() {
var avgGPA = 0;
for (var j = 0; j < this.gpa.length; j++) {
avgGPA = avgGPA + this.gpa[j];
}
avgGPA = avgGPA/this.gpa.length;
return avgGPA;
}
};
学校: 持有学生,并负责创建和打印他们
/**
* Object to encapsulate student creation while
* keeping track of all created students
* @returns {School}
*/
function School() {
/**
* Array of all the students created so far
* @type Array
*/
this.createdStudents = [];
}
School.prototype = {
/**
* Function to add a student to the list of students
* @param {Student} newStudent
* @returns {undefined}
*/
addStudent: function(newStudent) {
this.createdStudents.push(newStudent);
this.printScores();
},
/**
* Function to create a student, add it to the list, and return it
* @param {String} name
* @param {String} street
* @param {String} city
* @param {String} state
* @param {Array} gpa
* @returns {Student}
*/
createStudent: function(name, street, city, state, gpa) {
var newStudent = new Student(name, street, city, state, gpa);
this.addStudent(newStudent);
return newStudent;
},
/**
* Print out all the students and their scores
* @returns {undefined}
*/
printScores: function() {
for (var i = 0; i < this.createdStudents.length; i++) {
this.createdStudents[i].print();
}
}
};
主要的类: 什么测试学生的创造
/**
* The Factory to hold students
* @type School
*/
var school = new School();
// Student created yourself that you have access to in main
var standAloneStudent = new Student(
"Bob",
"555 Nowhere street",
"Timbucktue",
"AL",
[
4.0,
4.0,
4.0
]);
// You can get the students GPA without going through the factory
console.log("My student's GPA is: " + standAloneStudent.getAverageGPA());
console.log("");
// How to add a student that you create yourself
school.addStudent(new Student(
'Christian',
'5601 Pebble Beach Ln',
'Sacromento',
'CA',
[
2.5,
3.6,
3.8
]));
console.log("");
// How to create a student through the factory
school.createStudent(
"Walker",
'123 South Drive',
'Sarasota',
'FL',
[
3.0,
3.4,
3.8
]);
// You can also get the students GPA through the school
// (However, you may want to make a set instead of an array so you can key
// off of a student's name or maybe ID)
console.log("My student's GPA is: " + school.createdStudents[0].getAverageGPA());
这应该给你输出:
My student's GPA is: 4
Name: Christian
Address: 5601 Pebble Beach Ln Sacromento CA
Average GPA: 3.2999999999999994
Name: Christian
Address: 5601 Pebble Beach Ln Sacromento CA
Average GPA: 3.2999999999999994
Name: Walker
Address: 123 South Drive Sarasota FL
Average GPA: 3.4
My student's GPA is: 3.2999999999999994
什么是问题? –
请准确描述您遇到的问题以及您想要的帮助。 – jfriend00
当我运行stud1.test();我回来时出现了一个语法错误,指出stud1.test();不是函数 – user3072159