0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScriptでのモジュール化 import, export

Posted at
/modules/area.js
export function triange(base, height){
    return (base * height)/2;
}

export function circle(r){
    return r * r * Math.PI;
}
main.js
import { triange, circle } from './modules/area';

console.log(triange(3,8));
console.log(circle(10));

関数のほかに変数やクラスをexportすることもできる。

/modules/constants.js
const THRESHOLD = 100;
const MAIN_COLOR = 'purple';
const MODAL_WIDTH = 800;
const LIST_LENGTH = 100;
const DEFAULT_SORT = 'created_at';

export {THREHOLD, MAIN_COLOR, MODAL_WIDTH, LIST_LENGTH, DEFAULT_SORT };

1ファイル内で1つしかexportするものがない場合、上記のようにdefaultを使って明示できる。

export default class Triangle {
    constructor(base, height){
        this.base = base;
        this.height = height;
    }
    getArea(){
        return (this.base * this.height) / 2;
    }
    doubleBase(){
        this.base = this.base * 2;
    }
}

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?