I'm using classes and objects to create a list of stuff in javascript (I'm pretty new to programming). Now, I thought I had this right but for some reason it's throwing a
"Uncaught TypeError: courseList.calculateValue is not a function at assignment.js:27"
Can't figure out why this isn't working. Please help! Here's my code:
You have an object courseList with nested array courses, that actually contains objects of class Course.
courseList doesnt have this methods, because they were defined inside Course class. To execute this methods you need to do courseList.courses[0].calculateValue() and courseList.courses[1].calculateValue()
Looks like you're calling calculateValue()
on an object that is not a Course
object.
In your case you need to do courseList.courses[0].calculateValue()
to calculate the value of the first course. And likewise you can do it for other courses by changing the index.
Longer explanation
courseList
(badly named) is an object with just one property called courses
.
This property has a value which is a list of Course
objects. So in order to call methods from the Course
class on these objects you need to access them by first accessing the value of the course
property in courseList
and then choosing one of the courses from the list using an index like courseList.courses[1]
courseList.courses[1]
is now a Course
object so you can call calculateValue()
and printSummary()
on it
Side Note
Your console.log()
statement on line 28 won't print your results (even when you make the fix above) since printSummary
doesn't return anything. You don't need to call console.log()
on it at all, since printSummary
handles the printing within it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.