2017-05-12 1 views
0

나는 노드Node.js를 클래스 구문 에러 : 예기치 않은 식별자

class hello { 
function helloworld(){ 

     console.log('helloworld'); 


    } 



}; 

에 그런 내 클래스를 작성하지만 난 내 서버를 실행할 때 JS 클래스에 메소드를 정의 할 때이 오류

SyntaxError: Unexpected identifier

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

SyntaxError: Unexpected identifier

+0

';'를 제거하면 세미콜론을 제거한 후에 어떤 일이 발생합니까? – Jer

+0

@ C0dekid는 여전히 같은 오류가 있습니다 –

+0

함수 단어를 제거하십시오. 그럼 모든 것이 작동합니다. –

답변

1

를 얻을 수 function 키워드를 사용할 필요가 없습니다. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

간단하게 할 수 있습니다.

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

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

그런 다음 다른 파일.

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

이 읽기 ​​:이 도움이 What is the purpose of Node.js module.exports and how do you use it?

희망을.

+0

감사하지만 어떻게이 파일을 다른 방법으로 내보낼까요? –

관련 문제