| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/export | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since 20185.
export import
export { name1, name2, , nameN };
export { variable1 as name1, variable2 as name2, , nameN };
// var, const
export let name1, name2, , nameN;
export let name1 = , name2 = , , nameN;
// function class, function*
export default expression;
export default function () { }
export default function name1() { }
export { name1 as default, };
export * from ;
export { name1, name2, , nameN } from ;
export { import1 as name1, import2 as name2, , nameN } from ;
export named default named exports, default export export
Named exports:
//
export { myFunction };
//
export const foo = Math.sqrt(2);
export js :
export default function () {}
// 'export default class {}'
//
Named exports import , .
default export .
export default k = 12; // test.js import m from './test' // export default , import k console.log(m); // will log 12
export * from ;
import mod from "mod"; export default mod;
// module "my-module.js"
function cube(x) {
return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
var graph = {
options: {
color: "white",
thickness: "2px",
},
draw: function () {
console.log("From graph draw function");
},
};
export { cube, foo, graph };
//You should use this script in html with the type module ,
//eg ''<script type="module" src="demo.js"></script>",
//open the page in a httpserver,otherwise there will be a CORS policy error.
//script demo.js
import { cube, foo, graph } from "my-module";
graph.options = {
color: "blue",
thickness: "3px",
};
graph.draw();
console.log(cube(3)); // 27
console.log(foo); // 4.555806215962888
class fallback
// module "my-module.js"
export default function cube(x) {
return x * x * x;
}
import
import cube from "my-module";
console.log(cube(3)); // 27
Note var, let , const
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-exports |
importThis page was last modified on 2025716 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |