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

export - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

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 2018 5.

export JavaScript , , . import .

"use strict" . export HTML .

In this article

js
    //  
    export let name1, name2, , nameN; // var, const 
    export let name1 = , name2 = , , nameN; // var, const 
    export function functionName(){...}
    export class ClassName {...}

    //  
    export { name1, name2, , nameN };

    //   
    export { variable1 as name1, variable2 as name2, , nameN };

    //  
    export const { name1, name2: bar } = o;

    //  
    export default expression;
    export default function () {  } // also class, function*
    export default function name1() {  } // also class, function*
    export { name1 as default,  };

    //  
    export * from ; // does not set the default export
    export * as name1 from ;
    export { name1, name2, , nameN } from ;
    export { import1 as name1, import2 as name2, , nameN } from ;
    export { default } from ;
nameN

. import .

, (named) (default) . , . .

  • js
    //    
    export { myFunction, myVariable };
    
    //   
    // (, , , )
    export let myVariable = Math.sqrt(2);
    export function myFunction() { ... };
    
  • js
    //    
    export { myFunction as default };
    
    //   
    export default function () { ... };
    export default class { ... }
    

. .

.

js
// test.js
let k;
export default (k = 12);
js
//   
import m from "./test"; // k  ,   k  m   
console.log(m); // 12 

.

js
export { myFunction as function1, myVariable as variable };

/

. , .

js
export foo from "bar.js";

.

js
import foo from 'bar.js';
export foo;

.

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.
// For example:
//   <script type="module" src="./demo.js"></script>
//
// Open the page in a http server, 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

.

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

, :

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

export default var, let, const .

Specification
ECMAScript 2027 LanguageSpecification
# sec-exports


Web Proxy Viewer  |  New URL  |  Original Page