1. モジュールの作成(hello.js)
getCountという、オブジェクトを公開する、次の感じのコードを作成します
countは、公開されません
var count = 0;
module.exports = {
getCount: function() {
count++;
return count ;
}
};
2. モジュールを呼ぶ側の作成(sample5.js)
こんな感じです
var hello = require('./hello');
console.log(hello.getCount()) ;
console.log(hello.getCount()) ;
3.実行
・・・>node sample51
2
が表示されます。
0 件のコメント:
コメントを投稿