Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  45] [ 1]  / answers: 1 / hits: 101531  / 11 Years ago, tue, march 19, 2013, 12:00:00

So I am designing a grade book interface and I have a course defined as:



<script>
course = new Object();
var name;
var gradingareas;
var finalgrade;
</script>


then later I want to create a new instance:



 var gradingareas = new Array(Homework, Classwork, Exams);

course1 = new course(CS1500, gradingareas, 85);


I have also tried without the var in front to no avail. I get an Uncaught TypeError: Object is not a function I am very new to javascript so I don't even know if Im going about this the correct way. Any help is appreciated Thanks.


More From » object

 Answers
47

Your existing code:



// Creates a new, empty object, as a global
course = new Object();
// Creates three new variables in the global scope.
var name;
var gradingareas;
var finalgrade;


There is no connection between the variables and the object.



It looks like you want something more like:



function Course(name, gradingareas, finalgrade) {
this.name = name;
this.gradingareas = gradingareas;
this.finalgrade = finalgrade;
}


Then:



var course1 = new Course(CS1500, gradingareas, 85);


Note the use of a capital letter for naming the constructor function. This is a convention in the JS community.


[#79483] Monday, March 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryantc

Total Points: 455
Total Questions: 96
Total Answers: 110

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
bryantc questions
Fri, Aug 13, 21, 00:00, 3 Years ago
Tue, Mar 30, 21, 00:00, 3 Years ago
Fri, Jun 5, 20, 00:00, 4 Years ago
Wed, May 27, 20, 00:00, 4 Years ago
;