为什么在JavaScript中没有`import module-name.export1`? [英] Why not have `import module-name.export1` in javascript?
问题描述
ES6 import语句语法背后的设计限制是什么?
What is the design limitation behind the ES6 import statement syntax?
import { export1 } from "module-name";
如此处所示.
为什么没有
import module-name.export1
如果首先以某种方式具有'import'关键字非常重要,为什么不这样使用呢?
If somehow having the 'import' keyword in the first place is so important, why not use it like that?
为比较起见,请考虑其他几种流行的语言:
For comparison consider several other popular languages:
- Java:
import package.subpackage.ClassName;
- Python:
从模块导入SomeClass
- C#:
使用System.Text;
从左到右: broadScope->模块->specialItem .
在ES6中,它向后退: particularItem<-模块
首先编写 export1
,然后编写 module-name
更好?与 import module-name.export
相比,这有什么更好的选择?
How is it better to first write the export1
, then the module-name
?
How is that more optimal than import module-name.export
?
推荐答案
我不是其中之一-维护者.但是,这是一个简单的答案:
I am not one of them - maintainers. But, here's a straightforward answer:
您知道如何链接到文件夹吗?
You know how linking to a folder work?
'./project/dir'
链接到图像等时,我们使用类似的内容.这是HTML的标准.ES6也是HTML5(HTML,JavaScript,CSS)的一部分.因此,接下来需要从指定路径的文件夹中导入模块.
We use similar when linking to image, etc. It's a standard of HTML. And the ES6 is also a part of HTML5 (HTML, JavaScript, CSS). And thus, it follows to import modules from the folders specifying the path.
import defaultExport from '../path'
import { namedExport } from '../node_modules/package'
就像我们定义要使用的变量一样:
Like we define a variable to use:
// define myVar at first hand and declare it's value at second hand
const myVar = 10
console.log(myVar)
// But not
// 10 = const myVar
// console.log(10) // myVar
按照这种方法,我们有import语句:
Following the approach, we have the import statements:
import myVar from 'module' // value is extracted from module
import { myKeyVar } from 'package'
console.log(myVar, myKeyVar)
因此,我不能敦促具有如下定义:
So, I cannot urge to have definition like:
from 'module' import myVar
最后,没有其他方法可以做到这一点.因此,我们不能说这是最好的方法,也可能是最坏的方法.这只是他们遵循的标准.
And finally, there's no alternative way for this. So, we cannot say that this is the best approach or the worst approach. It's just a standard they follow to implement it.
这篇关于为什么在JavaScript中没有`import module-name.export1`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!