当前位置:网站首页>require与import的区别和使用
require与import的区别和使用
2022-07-01 21:32:00 【夏天想】
、require与import的区别和使用(CommonJS规范和es6规范)
1、import是ES6中的语法标准也是用来加载模块文件的,import函数可以读取并执行一个JavaScript文件,然后返回该模块的export命令指定输出的代码。export与export default均可用于导出常量、函数、文件、模块,export可以有多个,export default只能有一个。
2、require 定义模块:module变量代表当前模块,它的exports属性是对外的接口。通过exports可以将模块从模块中导出,其他文件加载该模块实际上就是读取module.exports变量,他们可以是变量、函数、对象等。在node中如果用exports进行导出的话系统会系统帮您转成module.exports的,只是导出需要定义导出名。
require与import的区别
1,require是CommonJS规范的模块化语法,import是ECMAScript 6规范的模块化语法;
2,require是运行时加载,import是编译时加载;
3,require可以写在代码的任意位置,import只能写在文件的最顶端且不可在条件语句或函数作用域中使用;
4,require通过module.exports导出的值就不能再变化,import通过export导出的值可以改变;
5;require通过module.exports导出的是exports对象,import通过export导出是指定输出的代码;
6,require运行时才引入模块的属性所以性能相对较低,import编译时引入模块的属性所所以性能稍高。
边栏推荐
- 分离字符串中的字母和数字并使得字母在前数组在后
- EURA eurui E1000 series inverter uses PID to realize the relevant parameter setting and wiring of constant pressure water supply function
- 中通笔试题:翻转字符串,例如abcd打印出dcba
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- 编译原理复习笔记
- PHP 读取ini或env类型配置
- 【深度学习】利用深度学习监控女朋友的微信聊天?
- 股票手机开户哪个app好,安全性较高的
- 选择在同花顺上炒股开户可以吗?安全吗?
- 多个张量与多个卷积核做卷积运算的输出结果
猜你喜欢
朋友圈社区程序源码分享
Principle of motion capture system
BC35&BC95 ONENET MQTT(旧)
Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan
【深度学习】利用深度学习监控女朋友的微信聊天?
leetcode刷题:栈与队列01(用栈实现队列)
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
leetcode刷题:二叉树02(二叉树的中序遍历)
新版图解网络PDF即将发布
Data analysts sound tall? Understand these points before you decide whether to transform
随机推荐
AirServer手机第三方投屏电脑软件
cmake工程化相关
【深度学习】利用深度学习监控女朋友的微信聊天?
Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
架构师毕业总结
图片拼图微信小程序源码_支持多模板制作和流量主
打出三位数的所有水仙花数「建议收藏」
编程英语生词笔记本
Halcon知识:三维重构的一个尝试
Comprehensive evaluation and detailed inventory of high-quality note taking software (I) note, obsedian, remnote, flowus
多个张量与多个卷积核做卷积运算的输出结果
leetcode刷题:栈与队列04(删除字符串中的所有相邻重复项)
旁路由设置的正确方式
《软件工程导论(第六版)》 张海藩 复习笔记
统计字符中每个字符出现的个数
喜马拉雅自研网关架构演进过程
Using qeventloop to realize synchronous waiting for the return of slot function
How can I know if I want to get the preferential link of stock account opening? Is it safe to open an account online?
300 linear algebra Lecture 4 linear equations
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“