[英]JavaScript Multi-Dimensional Array
I have a Student, who is in many courses, which have many Modules for each course. 我有一个学生,他上很多课程,每门课程都有很多模块。
So far, I have got: 到目前为止,我已经:
var myStudent = new MySchool.Student("Bart", "Simpson", "0800 Reverse", "Some Street", "Springfield");
myStudent.addCourse(new MySchool.Course("S1000", "Skateboarding"));
myStudent.addCourse(new MySchool.Course("Q1111", "Driving"));
myStudent.addCourse(new MySchool.Course("D999", "Avoiding Detention"));
Student.JS 学生JS
MyStudent.Student = function (firstName, lastName, tel, address1, address2) {
this._firstName = firstName;
this._lastName = lastName;
this._tel = tel;
this._address1 = address1;
this._address2 = address2;
this._courses = new Array();
};
//Add course:
addCourse: function (course) {
this._courses.push(course);
},
This works fine. 这很好。 However, I would like to add Modules to this.
但是,我想为此添加模块。 So for each course there are multiple modules.
因此,每门课程都有多个模块。
I have tried performing a multi-dimensional array but with no success. 我尝试执行多维数组,但没有成功。
Can someone please advise? 有人可以请教吗? Is there an alternative?
还有其他选择吗?
Not exactly sure what you mean exactly but from what I understood, here is how you could do : 不确定您的确切意思,但是据我了解,这是您可以执行的操作:
MyStudent.Student = function (firstName, lastName, tel, address1, address2) {
this._firstName = firstName;
this._lastName = lastName;
this._tel = tel;
this._address1 = address1;
this._address2 = address2;
this._courses = [];
this.addCourse = function (course) {
this._courses.push(new Course(course));
};
};
//Add course:
MySchool.Module = function(name){
this.name = name;
}
MySchool.Course = function(name) {
this.name = name;
this.modules = [];
this.addModule = function(name) {
this.mmodules.push(new MySchool.Module(name));
}
}
That way, you can create a student which has a function addCourse. 这样,您可以创建一个具有addCourse函数的学生。 Then you add whatever courses you want, and for each course you have a addModule function to fill them with modules.
然后,您可以添加所需的任何课程,并且对于每门课程,您都具有addModule函数,以用模块填充课程。
You could do something more complex, like for exemple creating a student which takes as parameters an array of courses/modules, which would look like that : 您可以做一些更复杂的事情,例如创建一个学生,该学生将一组课程/模块作为参数,如下所示:
courses = [
"poney" : [
"ride",
"jump"
],
"english" : [
"oral",
"grammar",
"written"
]
]
And then create a function which loop through the array and uses the addCourse and addModule functions to fill your student with his courses/modules. 然后创建一个遍历数组的函数,并使用addCourse和addModule函数为您的学生提供课程/模块。 But as you begin with JS, maybe you prefer the simple solution upside.
但是从JS开始时,也许您更喜欢简单的解决方案。
The OO way of doing this would be to have a modules
array in your course since the modules belong to the course. OO的方法是在课程中拥有一个
modules
数组,因为模块属于课程。 Then you would add modules to the course directly 然后,您可以将模块直接添加到课程中
Here's the quick and dirty: 这是快速又肮脏的:
addCourse: function (course) {
course.modules = [];
course.addModule = function(module){
this.modules.push(module);
}
this._courses.push(course);
return course;
}
which can be used like this: 可以这样使用:
myStudent.addCourse(new MySchool.Course("S1000", "Skateboarding")).addModule(...);
Of course the best approach is to deal with all of this inside the Course
constructor, which you haven't shown us. 当然,最好的方法是在
Course
构造函数中处理所有这些,您尚未显示给我们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.