javascript - Javascript 导入/导出语句
问题描述
在 javascript 中,它们是否都相似(第 1 部分和第 2 部分)?
[第1部分]
const sething = () => {
//Something
}
const sething1 = () => {
//Something
}
[第2部分]
const sething = () => {
//Something
}
export default sething
进而
import sething from /location
const sething1 = () => {
//Something
}
如果它们都不相似,有人可以告诉我为什么不相似吗?并建议阅读任何文章。
另外,如果我们为类做这样的事情,结果会怎样?
解决方案
我认为您在问:“第 1 部分是否等同于第 2 部分”,其中第 2 部分sething
在单独的文件中定义,然后将其导入到与sething1
.
我的回答是:
- 有一些方法是等价的
- 有一些方法是不同的
它们如何等效?
In both Part 1 and Part 2, you end up with sething
and sething1
defined as constants. If all you're concerned about is the behaviour of your application then for the most part I'd treat these as equivalent.
How are they different?
- If you re-use
sething
somewhere else, you'll simple be able to import it there. You can do that in Part 2 but not without refactoring or repeating yourself in Part 1. Or perhaps having one massive file. - If you wish to refactor the name
sething
, it'll be easier in Part 1 than in Part 2. In Part 2 most IDEs will change the imported name without changing the name of the constant inside the imported file. If it's used anywhere else, those uses will not be renamed. This can easily be fixed by using a named export instead of a default export.
I hope that helps and answers your question!
推荐阅读
- java - Apache Storm 服务停止
- javascript - 在socket.io编程中,所有客户端同时调用一个服务器
- javascript - 为什么 React 的状态机会更改不可变变量?
- c# - 为什么 Entity Framework Core 渴望加载未包含的相关实体
- reactjs - 是否总是在渲染之前调用 componentWillRecieveProps 还是以其他方式
- jquery - 如何手动向函数添加数据
- javascript - 我有一个对象数组和一个对象,我想循环一个对象,同时将对象值与数组中的值匹配
- javascript - 获得实际输出 JavaScript 后,我变得不确定
- java - 如何从随机二进制文件上传集合?
- spring-boot - 如何使用spring boot webSocket rabbitmq将数据从服务器推送到特定客户端