JavaScript中的'import'和'require'有什么区别? [英] What is the difference between 'import' and 'require' in JavaScript?

查看:131
本文介绍了JavaScript中的'import'和'require'有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中包含软件包的时候我想使用 import require ?它们是相同的还是具有不同的用例?

When including packages in JavaScript when would I want to use import vs. require? Are they the same or do they have distinct use cases?

推荐答案

您可以动态加载加载的模块名称不是预定义/静态,或者只有在真正需要时才有条件地加载模块(取决于某些代码流)。加载是同步的。这意味着如果您有多个需求,则会逐个加载和处理它们。 ES6导入:

You can have dynamic loading where the loaded module name isn't predefined /static, or where you conditionally load a module only if it's "truly required" (depending on certain code flow). Loading is synchronous. That means if you have multiple requires, they are loaded and processed one by one. ES6 Imports:

您可以使用命名导入来有选择地仅加载您需要的部分。这可以节省内存。导入可以是异步的(在当前的ES6模块加载器中,它实际上是)并且可以执行得更好。此外,Require模块系统不是基于标准的。现在ES6模块存在的可能性很小。在未来,将在各种实现中对ES6模块进行原生支持,这在性能方面将具有优势。

You can use named imports to selectively load only the pieces you need. That can save memory. Import can be asynchronous (and in current ES6 Module Loader, it in fact is) and can perform a little better. Also, the Require module system isn't standard based. It's is highly unlikely to become standard now that ES6 modules exist. In the future there will be native support for ES6 Modules in various implementations which will be advantageous in terms of performance.

这篇关于JavaScript中的'import'和'require'有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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