【问题标题】:JavaScript Multi-Dimensional ArrayJavaScript 多维数组
【发布时间】:2012-11-21 16:53:57
【问题描述】:

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

到目前为止,我得到了:

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);
},

这很好用。但是,我想为此添加模块。所以每门课程都有多个模块。

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

有人可以建议吗?有其他选择吗?

【问题讨论】:

  • 你的MySchool.Course呢?为什么不能为模块添加另一个数组?
  • “Some Street”应该是“742 Evergreen Terrace”
  • var sktCourse = new MySchool.Course(...); sktCourse.addModule(new MySchool.Module(...)); myStudent.addCourse(sktCourse);

标签: javascript arrays multidimensional-array


【解决方案1】:

不完全确定您的意思,但据我了解,您可以这样做:

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 时,也许您更喜欢简单的解决方案。

【讨论】:

    【解决方案2】:

    执行此操作的 OO 方法是在您的课程中包含一个 modules 数组,因为模块属于课程。然后你可以直接将模块添加到课程中

    【讨论】:

      【解决方案3】:

      这是又快又脏的:

      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(...);
      

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

      【讨论】:

      • ///constructor.Course = function (courseCode, name) { this._courseCode = courseCode; this._name = 名称; this._modules = new Array(); };
      猜你喜欢
      • 2017-05-24
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2011-01-17
      相关资源
      最近更新 更多