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

3
  • Remove ;, what happend after removing the semicolon? Commented May 12, 2017 at 11:45
  • @C0dekid still have the same error Commented May 12, 2017 at 11:48
  • Remove function word..all should work then. Commented May 12, 2017 at 11:49

1 Answer 1

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.

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

1 Comment

thanks but how i export this methods to other file ?

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.