0

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?

6
  • 2
    Why isn't courseList = []; in your constructor as this.courseList: = [];? Commented Jun 25, 2021 at 17:31
  • 1
    Is that supposed to be a variable shared by all class instances? You need to write static courseList = []; Commented Jun 25, 2021 at 17:32
  • 1
    But you can't access static variables with this, it should be User.courseList. Commented Jun 25, 2021 at 17:33
  • tried but not working @Barmar Commented Jun 25, 2021 at 17:37
  • Static class variables are only supported by newest browser versions. Commented Jun 25, 2021 at 17:41

1 Answer 1

1

You seem to be running an outdated version of node.js.

Support for public instance class fields was added in version 12.4.0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.