[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/import [Back]  [Original]

import() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

import()

20201

import() ECMAScript

js
import(moduleName)
import(moduleName, options)

import() import const myImport = import SyntaxError

options

moduleName

host-specified import

options

with

import

promise

  • moduleName
  • moduleName
  • moduleName Node Error TypeError

import()

import import something from "somewhere" import syntactic rigidity

  • eval

HTML type="module"

options import

js
import("./data.json", { with: { type: "json" } });

import() / worker service worker worklet

import * as name from moduleName

null Array.prototype.sort() default "Module" [Symbol.toStringTag] Object.prototype.toString()

Object.getOwnPropertyDescriptors()

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

import("/my-module.js").then((mod2) => {
  console.log(mod === mod2); // true
});

promise thenable my-module.js then() promise promise

js
// my-module.js
export function then(resolve) {
  console.log("then() ");
  resolve(1);
}
js
// main.js
import * as mod from "/my-module.js";

import("/my-module.js").then((mod2) => {
  // then() 
  console.log(mod === mod2); // false
});

then()

js
(async () => {
  if (somethingIsTrue) {
    // 
    await import("/modules/my-module.js");
  }
})();

ESM

default default

js
(async () => {
  if (somethingIsTrue) {
    const {
      default: myDefault,
      foo,
      bar,
    } = await import("/modules/my-module.js");
  }
})();

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;
      });
  });
}

document navigator Web API

js
let myModule;

if (typeof window === "undefined") {
  myModule = await import("module-used-on-server");
} else {
  myModule = await import("module-used-in-browser");
}

10 /modules/module-0.js/modules/module-1.js load

js
Promise.all(
  Array.from({ length: 10 }).map(
    (_, index) => import(`/modules/module-${index}.js`),
  ),
).then((modules) => modules.forEach((module) => module.load()));

ECMAScript 2027 LanguageSpecification
# sec-import-calls


Web Proxy Viewer  |  New URL  |  Original Page