Hướng dẫn dùng carspeed JavaScript

The static import declaration is used to import read-only live bindings which are exported by another module. The imported bindings are called live bindings because they are updated by the module that exported the binding, but cannot be modified by the importing module.

In order to use the import declaration in a source file, the file must be interpreted by the runtime as a module. In HTML, this is done by adding type="module" to the

It is also possible to specify a default import with namespace imports or named imports. In such cases, the default import will have to be declared first. For instance:

import myDefault, * as myModule from "/modules/my-module.js";
// myModule.default and myDefault point to the same binding

or

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

Importing a name called default has the same effect as a default import. It is necessary to alias the name because default is a reserved word.

import { default as myDefault } from "/modules/my-module.js";

Namespace import

The following code inserts myModule into the current scope, containing all the exports from the module located at /modules/my-module.js.

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

Here, myModule represents a namespace object which contains all exports as properties. For example, if the module imported above includes an export doAllTheAmazingThings(), you would call it like this:

myModule.doAllTheAmazingThings();

myModule is a sealed object with null prototype. All keys are enumerable in lexicographic order (i.e. the default behavior of Array.prototype.sort()), with the default export available as a key called default.

Note: JavaScript does not have wildcard imports like import * from "module-name", because of the high possibility of name conflicts.

Import a module for its side effects only

Import an entire module for side effects only, without importing anything. This runs the module's global code, but doesn't actually import any values.

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

This is often used for polyfills, which mutate the global variables.

Examples

Standard Import

In this example, we create a re-usable module that exports a function to get all primes within a given range.

// getPrimes.js
/**
 * Returns a list of prime numbers that are smaller than `max`.
 */
export function getPrimes(max) {
  const isPrime = Array.from({ length: max }, () => true);
  isPrime[0] = isPrime[1] = false;
  isPrime[2] = true;
  for (let i = 2; i * i < max; i++) {
    if (isPrime[i]) {
      for (let j = i ** 2; j < max; j += i) {
        isPrime[j] = false;
      }
    }
  }
  return [...isPrime.entries()]
    .filter(([, isPrime]) => isPrime)
    .map(([number]) => number);
}

import { getPrimes } from "/modules/getPrimes.js";

console.log(getPrimes(10)); // [2, 3, 5, 7]

Imported values can only be modified by the exporter

The identifier being imported is a live binding, because the module exporting it may mutate it and the imported value would change. However, the module importing it cannot re-assign it.

// my-module.js
export let myValue = 1;
setTimeout(() => {
  myValue = 2;
}, 500);

// main.js
import { myValue } from "/modules/my-module.js";
console.log(myValue); // 1
setTimeout(() => {
  console.log(myValue); // 2; my-module has updated its value
  myValue = 3; // TypeError: Assignment to constant variable.
  // The importing module can only read the value but can't re-assign it.
}, 1000);

Specifications

Specification
ECMAScript Language Specification
# sec-imports

Browser compatibility

BCD tables only load in the browser

See also

  • export
  • Dynamic imports
  • import.meta
  • Limin Zhu, Brian Terlson and Microsoft Edge Team: Previewing ES6 Modules and more from ES2015, ES2016 and beyond
  • Hacks blog post by Jason Orendorff: ES6 in Depth: Modules
  • Hacks blog post by Lin Clark: ES modules: A cartoon deep-dive
  • Axel Rauschmayer's book: "Exploring JS: Modules"
  • The Modern JavaScript Tutorial(javascript.info): Export and Import