JavaScript多维数组

JavaScript多维数组,javascript,arrays,multidimensional-array,Javascript,Arrays,Multidimensional Array,我有一个学生,他参加了很多课程,每门课程都有很多模块 到目前为止,我得到了: var myStudent = new MySchool.Student("Bart", "Simpson", "0800 Reverse", "Some Street", "Springfield"); myStudent.addCourse(new MySchool.Course("S1000", "Skateboarding")); myStudent.addCourse(new MySchool.Course

我有一个学生,他参加了很多课程,每门课程都有很多模块

到目前为止,我得到了:

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

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);
},
这个很好用。但是,我想在此基础上添加模块。因此,每门课程都有多个模块

我尝试过执行多维数组,但没有成功


有人能给点建议吗?有其他选择吗?

这样做的OO方法是在课程中设置一个
模块
数组,因为模块属于课程。然后你会直接在课程中添加模块

不确定你的确切意思,但根据我的理解,你可以这样做:

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));
    }
}
这样,您就可以创建一个具有函数addCourse的学生。 然后你可以添加你想要的任何课程,对于每门课程,你都有一个addModule函数来用模块填充它们

您可以做一些更复杂的事情,例如创建一个学生,将一系列课程/模块作为参数,如下所示:

courses = [
    "poney" : [
        "ride",
        "jump"
    ],
    "english" : [
        "oral",
        "grammar",
        "written"
    ]
]

然后创建一个函数,该函数在数组中循环,并使用addCourse和addModule函数向您的学生填充课程/模块。但是,当您开始使用JS时,您可能更喜欢简单的解决方案。

以下是快速和肮脏的解决方案:

addCourse: function (course) {
    course.modules = [];
    course.addModule = function(module){
        this.modules.push(module);
    }
    this._courses.push(course);
    return course;
}
可以这样使用:

myStudent.addCourse(new MySchool.Course("S1000", "Skateboarding")).addModule(...);

当然,最好的方法是在
课程
构造函数中处理所有这些,您还没有向我们展示过。

您的
MySchool.course
在哪里?为什么不能为模块添加另一个数组?“Some Street”应该是“742 Evergreen Terrace”
var sktCourse=new MySchool.Course(…);sktCourse.addModule(新的MySchool.Module(…);myStudent.addCourse(sktCourse)///constructor.Course=function(courseCode,name){this.\u courseCode=courseCode;this.\u name=name;this.\u modules=new Array();};