0

I write my class like that on node

class hello {
 function helloworld(){

       console.log('helloworld');


   }



};

but when I run my server i get this error

SyntaxError: Unexpected identifier

function helloworld(id){ ^^^^^^^^^^

SyntaxError: Unexpected identifier

dark night
  • 171
  • 4
  • 19

1 Answers1

1

When you defining method in JS classes you don't need to use function keyword. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

You can simply just do.

class hello {
     helloworld () {
        console.log('helloworld');
     }
}

var a = new hello();
a.helloworld();
//to export from file
exports.hello = hello;

Then in other file.

var myClass = require('yourModule');
var a = new myClass.hello();
a.helloworld();

Read this: What is the purpose of Node.js module.exports and how do you use it?

Hope this helps.

Community
  • 1
  • 1
Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24