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

import - 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

import

Baseline Widely available *

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

* Some parts of this feature may have varying levels of support.

import , . , . script type="module".

function-like import(), "module".

nomodule script.

, . tree shaking.

: . , TypeScript Babel, , , Rollup Webpack.

In this article

import defaultExport from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { export1 , export2 as alias2 , [] } from "module-name";
import defaultExport, { export [ , [] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
import("/module-name.js").then(module => {}) //  
defaultExport

, ( ) .

module-name

. .js .js. ; . .

name

, , .

export, exportN

, .

alias, aliasN

, .

name , , . export , import * as name . .

myModule , , /modules/my-module.js.

js
import * as myModule from "/modules/my-module.js";

, ( "myModule") . , doAllTheAmazingThings(), :

js
myModule.doAllTheAmazingThings();

, myExport, my-module ( ), ( export), myExport .

js
import { myExport } from "/modules/my-module.js";

foo bar .

js
import { foo, bar } from "/modules/my-module.js";

, . , shortName .

js
import { reallyReallyLongModuleExportName as shortName } from "/modules/my-module.js";

, , .

js
import {
  reallyReallyLongModuleExportName as shortName,
  anotherLongModuleName as short,
} from "/modules/my-module.js";

, -. , .

js
import "/modules/my-module.js";

export ( , , .). import .

:

js
import myDefault from "/modules/my-module.js";

( ). , . :

js
import myDefault, * as myModule from "/modules/my-module.js";
// myModule    

js
import myDefault, { foo, bar } from "/modules/my-module.js";
//    

, .

:

  • my-module.js:

    js
    export let a = 2;
    export let b = 3;
    
  • main.js:

    js
    import { a, b } from "/modules/my-module.js";
    a = 5;
    b = 6;
    // Uncaught TypeError: Assignment to constant variable.
    

.

:

  • my-module.js:

    js
    export let obj = { a: 2, b: 4 };
    
  • main.js:

    js
    import { obj } from "/modules/my-module.js";
    
    obj.a = 1;
    obj.b = 4;
    

, import , , .

import . import() Promise.

js
import("/modules/my-module.js").then((module) => {
  //  -  
});

Promise, await

js
let module = await import("/modules/my-module.js");

, , , , .. Function.prototype , , .call, .apply .bind

AJAX JSON.

: file.js

js
function getJSON(url, callback) {
  let xhr = new XMLHttpRequest();
  xhr.onload = function () {
    callback(this.responseText);
  };
  xhr.open("GET", url, true);
  xhr.send();
}

export function getUsefulContents(url, callback) {
  getJSON(url, (data) => callback(JSON.parse(data)));
}

: main.js

js
import { getUsefulContents } from "/modules/file.js";

getUsefulContents("http://www.example.com", (data) => {
  doSomethingUseful(data);
});

, ( , ), . - . import() await.

js
const main = document.querySelector("main");
for (const link of document.querySelectorAll("nav > a")) {
  link.addEventListener("click", (e) => {
    e.preventDefault();

    import("/modules/my-module.js")
      .then((module) => {
        module.loadPageInto(main);
      })
      .catch((err) => {
        main.textContent = err.message;
      });
  });
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-imports


Web Proxy Viewer  |  New URL  |  Original Page