ES6 module
module(export, import) ์ดํด - babel ์ค์ ์ด ํ์ํจ
// dist/bundle.js
import { log } from './myLogger' // myLogger ํ์ผ์ด ๊ฐ์ฒด์ ํํ๋ก ๋์ด์ค๊ธฐ ๋๋ฌธ์ destructing ํํ๋ก log func๋ง ๋ฝ์์ ์ด๋ค
const root = document.querySelector('#root')
root.innerHTML = `<p>Hello world!</p>`
log()
// myLogger.js
export default function log(data) { // export default๋ก export. default๋ ์ด ํ์ผ ๋ด์ ์ ์ผํ export
console.log(data)
}
module(export, import) ๊ธฐ๋ฐ ์๋น์ค ๊ตฌํ
// bundle.js
import log, { getTime, getCurrentHour, MyLogger, _ } from './myLogger'
const root = document.querySelector('#root')
root.innerHTML = `<p>Hello world!</p>`
log('test 1234')
log(`now time is ${getTime()}`)
log(`current hour is ${getCurrentHour()}`)
const logger = new MyLogger()
log(`my lectures are ${logger.getLectures()}`)
_.log('hihi')
// myLogger.js
// export default function log(data) {
// console.log(data)
// }
export const getTime = () => {
return Date.now()
}
export const getCurrentHour = () => {
return (new Date).getHours()
}
export default class MyLogger {
constructor(props) {
this.lectures = ['java', 'IOS']
}
getLectures() {
return this.lectures
}
}
/* utility */
export const _ = {
log(data) {
if(window.console) console.log(data)
}
}
Last updated