I have two files in the same directory as 06classjs.js and 07 classObjects.js
06classjs.js
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
courseList = [];
getInfo() {
return { name: this.name, email: this.email };
}
enrollCourse(name) {
this.courseList.push(name);
}
getCourseList() {
return this.courseList;
}
}
module.exports = User;
07 classObjects.js
const User = require("./06classjs.js");
const dilip = new User("Dilip", "[email protected]");
console.log(dilip.getInfo());
dilip.enrollCourse("React");
When I am trying to run 07 classObjects.js using node it gives a syntax error for array declaration.
Error log
courseList = [];
^
SyntaxError: Unexpected token =
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/media/dilip/4E622DC7622DB49D/Courses/LCO/JSTube/06 Advanceish/07 classObjects.js:3:14)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
Can anyone help me with this?
courseList = [];in your constructor asthis.courseList: = [];?static courseList = [];this, it should beUser.courseList.