ES6 Class Importing & Exporting

最近在搞 blizzard_forum.js,打算寫 ES6 語法來學學新知。因為 Node.js 還沒原生支援 ES6 syntax,就順便用用看 Babel 囉。

今天寫 ES6 class import/export 遇到了一些問題,在網上找好久找不太到答案(明明就是你懶得仔細看官方 doc),在這邊筆記一下。

export class

class.js:

export class i_am_the_class_name {  
    bar () { console.log("foobar") }
}

import.js:

import {i_am_the_class_name as what_ever_varname_you_want} from '/path/to/class.js'  
let myClass = new what_ever_varname_you_want()  
myClass.bar() // foobar  

export default class

class.js:

export default class i_am_the_class_name {  
    bar () { console.log("foobar") }
}

import.js:

import what_ever_varname_you_want from '/path/to/class.js'  
let myClass = new what_ever_varname_you_want()  
myClass.bar() // foobar