javascript中的构造函数和原型

javascript中的构造函数和原型,javascript,Javascript,我很难学习构造函数和原型。我所要做的就是向构造中添加两个新对象,然后显示对象的每个部分。在我的示例中,我想添加两个对象和控制台日志,分别是名称、地址和gpa 建造商: var Students = function(name, street, city, state, gpa) { this.student = { name: name, address: { street: street, city: city,

我很难学习构造函数和原型。我所要做的就是向构造中添加两个新对象,然后显示对象的每个部分。在我的示例中,我想添加两个对象和控制台日志,分别是名称、地址和gpa

建造商:

var Students = function(name, street, city, state, gpa) {
   this.student = {
      name: name,
      address: {
         street: street,
         city: city,
         state: state
      },
      gpa: gpa};
   this.array = [];
   this.array.push(this.student);
};

Students.prototype = {
   init: function(name, street, city, state, gpa) {
      this.student = {
         name: name,
         address: {
            street: street,
            city: city,
            state: state
         },
         gpa: gpa
      };
      this.array = [];
      this.array.push(this.student);
      console.log(this.array);
   },
   test: function() {
      console.log(this.array);
      for (i = 0; i < this.array.length; i++) {
         i = i % this.array.length;
         var studentTotal = 0;
         //This will loop through gpa array
         for (j = 0; j < this.array[i].gpa.length; j++) {
            studentTotal += this.array[i].gpa[j];
         }

         this.array[i].myAvg = studentTotal / this.array[i].gpa.length;
         this.array[i].gpaAverage = this.array[i].myAvg;
         console.log("Name: " + this.array[i].name);
         console.log("Address: " + this.array[i].address.street + ' ' +
                 this.array[i].address.city + ' ' +
                 this.array[i].address.state);
         console.log("GPA: " + this.array[i].gpa);
         console.log("Average: " + this.array[i].gpaAverage);
         console.log(new Date());
      }
   }
};

您正在构建一组学生

var stud1=[…]
正在创建一个
Student
数组,我想最好将
Student
函数重命名为
Student
(因为它只构造一个Student),并将数组
stud1
重命名为
studentArray
,以使事情更容易理解

然后,如果要对数组中的所有学生调用
test()
,请使用:

for(var i=0; i<studentArray.length; i++){
   studentArray[i].test();
}
学校: 容纳学生并负责创建和打印的班级

/**
 * 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();
      }
   }
};
这将为您提供以下输出:

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

问题是什么?请准确描述您遇到的问题以及您希望得到的帮助。当我运行stud1.test()时;我返回时出现一个语法错误,指出stud1.test();不是函数
stud1
只是一个普通数组。它只有数组的方法。我如何才能添加新对象,并且在创建新对象时,控制台记录所有对象,包括以前添加的对象?因此,我可以摆脱原始对象,只使用此对象吗?我正在尝试控制台记录对象的每个单独部分。我希望能够控制台记录每个对象的名称、每个对象的地址等。。这就是我迷路的地方,我不知道该怎么办。你需要写一个函数来打印学生的每个变量。我会编辑我的答案,告诉你怎么做。好的,那太好了
/**
 * 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