[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/export [Back]  [Original]

export - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

export

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 20185.

export import

embedded script

In this article

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 ;
nameN

import

export named default named exports, default export export

  • Named exports:

    js
    // 
    export { myFunction };
    
    // 
    export const foo = Math.sqrt(2);
    
  • export js :

    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;

js
// 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 };

js
//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

js
// module "my-module.js"
export default function cube(x) {
  return x * x * x;
}

import

js
import cube from "my-module";
console.log(cube(3)); // 27

Note var, let , const

Specification
ECMAScript 2027 LanguageSpecification
# sec-exports


Web Proxy Viewer  |  New URL  |  Original Page