Last active
January 9, 2017 04:52
-
-
Save eMahtab/018ccae028273202a5580a00cb964bab to your computer and use it in GitHub Desktop.
ES6 Exporting and Importing Modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exporting //math/addition.js | |
function sumTwo(a, b) { | |
return a + b; | |
} | |
function sumThree(a, b, c) { | |
return a + b + c; | |
} | |
export {sumTwo,sumThree}; | |
Importing // main.js | |
import {sumTwo,sumThree} from 'math/addition.js'; | |
console.log("sumTwo(1,10) = "+sumTwo(1,10)); | |
console.log("sumThree(1,10,3) = "+sumThree(1,10,3)); | |
================================================================================================================ | |
Exporting //math/addition.js | |
export function sumTwo(a, b) { | |
return a + b; | |
} | |
function sumThree(a, b, c) { | |
return a + b + c; | |
} | |
export {sumThree}; | |
Importing // main.js | |
import {sumTwo,sumThree} from 'math/addition.js'; | |
console.log("sumTwo(11,10) = "+sumTwo(11,10)); | |
console.log("sumThree(1,10,3) = "+sumThree(1,10,3)); | |
===================================================================================================================== | |
Exporting //math/addition.js | |
export function sumTwo(a, b) { | |
return a + b; | |
} | |
export function sumThree(a, b, c) { | |
return a + b + c; | |
} | |
Importing // main.js | |
import {sumTwo as addTwo,sumThree} from 'math/addition.js'; | |
console.log("addTwo(19,10) = "+addTwo(19,10)); | |
console.log("sumThree(1,10,1) = "+sumThree(1,10,1)); | |
======================================================================================================================== | |
Exporting //math/addition.js | |
export function sumTwo(a, b) { | |
return a + b; | |
} | |
export function sumThree(a, b, c) { | |
return a + b + c; | |
} | |
Importing // main.js | |
import * as utils from 'math/addition.js'; | |
console.log("sumTwo(19,-10) = "+ utils.sumTwo(19,-10)); | |
console.log("sumThree(1,10,-1) = "+ utils.sumThree(1,10,-1)); | |
========================================== Export default ============================================================== | |
Exporting //math/addition.js | |
export default function sumTwo(a, b) { | |
return a + b; | |
} | |
Importing // main.js | |
import sumTwo from 'math/addition.js'; | |
console.log("sumTwo(100,-10) = "+sumTwo(100,-10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment