在ES6中有资格作为动态出口 [英] What qualifies as being a dynamic export in ES6

查看:113
本文介绍了在ES6中有资格作为动态出口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说es6中不允许动态导出/导入。

I hear that dynamic exports/imports are not allowed in es6.

本网站使用示例出口默认5 * 7; 好像是合法的静态导出。这似乎是合理的,因为它清楚地评估为35的静态值,但我想知道现在是什么究竟是静态导出的。

This website Uses the example export default 5 * 7; as if it were a legal, static export. This seems reasonable since it clearly evaluates to the static value of 35, but I'm wondering what exactly qualifies as a static export now.

该代码使用 export default Backbone.Router.extend({。 ..}); 好像是一个合法的,静态的,导出的。这似乎对我来说很像是对我的动态导出(导出函数调用的结果)。

This Code uses export default Backbone.Router.extend({...}); as if it were a legal, static, export. This seems fishy to me as it seems like a dynamic export to me (exporting the result of a function call).

推荐答案

所有导出在ES6中是静态的,也就是说,它们的导出名称解析出导出模块中的一个变量绑定,这可以在评估模块代码之前由单一的外观来确定。

All exports are static in ES6, that is, their exported name resolves to exactly one variable binding in the exporting module and this can be determined by a single look prior to evaluating of the module code.

模块不能通过执行代码动态添加或删除导出,导出的名称列表由声明修复。

A module cannot dynamically add or remove exports through execution of code, the list of exported names is fixed by the declaration.

此变量是否保存常量或函数调用的结果并不重要,也不管它是否保存原始值或对象。它甚至不需要是一个常量,变量的内容可能随着时间的推移而改变(参见此处的示例))

Whether this variable holds a constant or the result of a function call doesn't matter, neither does whether it holds a primitive value or an object. It doesn't even need to be a constant, the content of the variable may change over time (see an example here).

import 语句中导入的所有内容也是静态的,这意味着您可以在不执行任何模块代码的情况下探索依赖图。

All imports from import statements are static as well, which means that you can explore the dependency graph without executing any module code.

动态导入是通过显式调用模块加载器完成的。这种负载可以取决于模块的控制流程,并且可能因运行而异。代码需要手动处理进程的异步和潜在错误。

A dynamic import is done by an explicit call to the module loader. Such loads can depend on the control flow of the module, and may differ from run to run. The code needs to manually handle the asynchrony of the process and potential errors.

这篇关于在ES6中有资格作为动态出口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆