LoginSignup
43
33

More than 5 years have passed since last update.

Node.jsでClassを別ファイルに分割する(2017)

Last updated at Posted at 2017-12-24

module.exportsの書く場所に注意しなければならない。情報が錯綜していてなかなかうまく行かなかった。

How to

classess.js
module.exports = class Animal{
    constructor (baw) {
         this.baw = baw;
    }
    say (){
        console.log(this.baw);
    }
}

module.exportsに直接classを代入していることに注意。

app.js
const Animal = require('./classess.js');
var inu  = new Animal('わんわん');

inu.say();   // → わんわん

以上。

43
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
33