The module system allows users to write import and export statements in scripts, which can be used to separate the logic of the application into custom modules. The standard's relevant part can be found here. Embedders wishing to use native builtin modules with ES6 imports can use the Port API to do so.
If a script contains import statements, then JerryScript will open and evaluate the the referenced modules before the main script runs, resolving and creating bindings for the referenced identifiers in the process. It is not necessary to use any specific filename extensions for modules, JerryScript will try to open the given file paths as they are, but will try to normalize them before doing so. The exact normalization process is dependant on the port implementation provided. It is the user's responsibility to verify that the given files are valid EcmaScript modules.
main.js
import { exported_value } from "./module.js"
print (exported_value);
module.js
var exported_value = 42;
export exported_value;
import * as module from 'module.js
export {variable} from 'module.js'
export * from 'module.js'
import 'module.js'
export default local_identifier
import def from 'module.js'
export default function () {}
import {
engine,
version as v
} from "./module.js"
import { getFeatureDetails } from "./module_2.js"
var version = "v3.1415";
print("> main.js");
print(">> Engine: " + engine);
print(">> Version: " + v);
print (">> " + getFeatureDetails());
print (">> Script version: " + version);
// module.js
var _engine = "JerryScript";
export _engine as engine;
export var version = "1.0 (e92ae0fb)";
// module_2.js
var featureName = "EcmaScript 2015 modules";
var year = 2018;
export function getFeatureDetails() {
return "Feature name: " + featureName + " | developed in " + year;
}
A module namespace object can be imported. In this case the local binding will contain an object holding the exported values of the module, including local exports and all indirect exports. Ambiguous exported names are exluded from the namespace object.
import * as module from './module.js';
print(">> Engine: " + module.engine);
print(">> Version: " + module.version);
An export statement can transitively export variables from another module, either via named indirect exports or a star export statement. In this case the resolving process will follow the chain until it reaches a module containing a local binding for that export name. If there are multiple modules which satisfy the export, that means the export is ambiguous, and will result in a SyntaxError.
import { a, b } from 'module.js'
print (a + b);
// module.js
export var a = 2;
export { b } from 'module2.js'
// module2.js
export var b = 40;
Each module can optionally provide a single default export by using the export default
statement. Default exports can either reference identifiers in the module's lexical environment, or be an anonymous default export, in which case they will only be accessible by an importing script.
import defaultExport, { b as c } from 'module.js'
print (defaultExport); // 2
print (c ()); // 42
// module.js
export default 2;
export function b () {
return 42;
}
Evaluate a module without importing anything. Any errors encountered in the module will be propagated.
import 'module.js' // > module.js
// "> module.js" is printed
b (); // (ReferenceError) b is not defined
// module.js
export function b () {
print ("> module.js");
return 42;
}
b ();
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Комментарий ( 0 )