| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/import | [Back] [Original] |
Get to know MDN better
import(moduleName)
import(moduleName, options)
import() import const myImport = import SyntaxError
moduleNamehost-specified import
optionspromise
import()
import import something from "somewhere" import syntactic rigidity
eval HTML type="module"
options import
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()
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
// my-module.js
export function then(resolve) {
console.log("then() ");
resolve(1);
}
// main.js
import * as mod from "/my-module.js";
import("/my-module.js").then((mod2) => {
// then()
console.log(mod === mod2); // false
});
then()
(async () => {
if (somethingIsTrue) {
//
await import("/modules/my-module.js");
}
})();
ESM
default default
(async () => {
if (somethingIsTrue) {
const {
default: myDefault,
foo,
bar,
} = await import("/modules/my-module.js");
}
})();
import() await
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
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
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 |