Home > Software engineering >  node cross-env can't set the class variables
node cross-env can't set the class variables

Time:05-26

i am using node v12.22.9, npm 8.5.1 and installed cross-env: "^7.0"

ERROR in ./resources/js/frontend/eyesonMeeting.js
Module build failed: SyntaxError: Unexpected token (4:10)

  2 | 
  3 |  class eyesonMeeting {
> 4 |     audio = true
    |           ^

how can i set the class variables without a conflict? i have tried to replace cross-env by laravel-mix but i faced a conflict with sass.

package.json scripts:

"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",

CodePudding user response:

When you're using classes you can use the constructor to set class variables using this

class eyesonMeeting {
  constructor() {
    this.audio = true;
  }

  // example using audio in a different function
  someFunctionSettingAudioFalse() {
    this.audio = false;
  }

  hasAudio() {
    return this.audio;
  }
}
  • Related