2017-05-12 7 views
0

は私がノードNode.jsのクラスにSyntaxError:予期しない識別子

class hello { 
function helloworld(){ 

     console.log('helloworld'); 


    } 



}; 

にそのような私のクラスを書くが、私は自分のサーバーを実行するときは、JSクラスにメソッドを定義するとき、私はこのエラー

SyntaxError: Unexpected identifier

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

SyntaxError: Unexpected identifier

+0

';'を削除すると、セミコロンを削除した後に何が起こりますか? – Jer

+0

@ C0dekidにはまだ同じエラーがあります –

+0

function word..allを削除してください。 –

答えて

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

ありがとう、しかし、この方法を他のファイルにどのようにエクスポートするのですか? –

関連する問題