当前位置:网站首页>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编译时引入模块的属性所所以性能稍高。
边栏推荐
猜你喜欢
随机推荐
多个张量与多个卷积核做卷积运算的输出结果
Vulnerability recurrence - Net ueeditor upload
柒微自动发卡系统源码
杰理之、产线装配环节【篇】
BC35&BC95 ONENET MQTT(旧)
leetcode刷题:二叉树01(二叉树的前序遍历)
Customize the insertion of page labels and realize the initial search of similar address books
MySQL清空表数据
leetcode刷题:栈与队列06(前 K 个高频元素)
个人炒股怎样开户?安全吗。
Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
开环和闭环是什么意思?
Data analysts sound tall? Understand these points before you decide whether to transform
深度学习 神经网络基础
There are four ways to write switch, you know
idea中类中显示成员变量和方法
leetcode刷题:二叉树03(二叉树的后序遍历)
cmake工程化相关
东哥套现,大佬隐退?
《軟件工程導論(第六版)》 張海藩 複習筆記