2013-12-20 70 views
0

我很难学习构造函数和原型。我想要做的是将2个新对象添加到我的构造中,然后显示对象的每个部分。在我的例子中,我想添加两个对象和控制台日志每个名称,地址和GPA。javascript中的构造函数和原型

构造:

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

main.js:

var stud1 = [ 
    new Students(
      "Walker", 
      '123 South Drive', 
      'Sarasota', 
      'FL', 
      [ 
       3.0, 
       3.4, 
       3.8 
      ]), 
    new Students(
      'Christian', 
      '5601 Pebble Beach Ln', 
      'Sacromento', 
      'CA', [ 
     2.5, 3.6, 3.8 
    ])]; 

stud1.test(); 
+3

什么是问题? –

+1

请准确描述您遇到的问题以及您想要的帮助。 – jfriend00

+0

当我运行stud1.test();我回来时出现了一个语法错误,指出stud1.test();不是函数 – user3072159

回答

1

您正在构建学生的数组。

var stud1 = [...]创造的Students数组我想像它会更最好的Students功能重命名为Student(因为它只有一个构建学生)和重命名你的数组stud1studentArray使事情变得更容易理解。

然后,如果你想调用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 
+0

我怎么才能添加新对象,并且在创建新对象时,控制台会记录所有对象,包括以前添加的对象? – user3072159

+0

所以,我可以摆脱我原来的对象,只使用这一个? – user3072159

+0

我正在尝试控制台记录对象的每个单独部分。我希望能够控制登录每个对象的名称,每个对象的地址等。这是我迷失方向,不知道该怎么做的地方。 – user3072159