简体   繁体   English

新的javascript ES6模块术语中的合格和不合格导入有什么区别?

[英]What is the difference between qualified and unqualified imports in the new javascript ES6 module lingo?

I came across this distinction which wasn't explained well in ExploringJS 我遇到了这个在ExploringJS中没有得到很好解释的区别

Qualified and unqualified imports work the same way (they are both indirections) 合格和不合格的进口工作方式相同(它们都是间接的)

What is the distinction and therefore what does this statement mean? 区别是什么,因此该陈述的含义是什么?

Strictly speaking, there's no such thing as qualified/unqualified imports in JavaScrpit. 严格来说,JavaScrpit中没有合格/不合格的导入。 Those terms were used in the book 'Exploring ES6' by Dr. Axel Rauschmayer in the context of cyclic dependencies, and roughly mean: Axel Rauschmayer博士在循环依赖的背景下,在“探索ES6”一书中使用了这些术语,大致意思是:

Unqualified imports (directly import a part of a module): 不合格的导入(直接导入模块的一部分):

CommonJS CommonJS的

var foo = require('a').foo // doesn't work with cyclic dependencies

ES2015 ES2015

import {foo} from 'a' // can work with cyclic dependencies*

Qualified imports (import the whole module as a namespace): 合格导入(将整个模块导入为命名空间):

CommonJS CommonJS的

var a = require('a')
function bar() {
  a.foo() // can work with cyclic dependencies*
}
exports.bar = bar

ES2015 ES2015

import * as a from 'a'
export function bar() {
  a.foo() // can work with cyclic dependencies*
}

In ES2015 default imports can also be qualified imports (although some people disagree) if they serve as a namespace: 在ES2015中,默认导入也可以是限定导入(尽管有些人不同意),如果它们用作命名空间:

export default {
  fn1,
  fn2
}

*with cyclic dependencies, you can't access imports in the body of a module: *具有循环依赖关系,您无法访问模块正文中的导入:

import {foo} from 'a' // 'a' is a cyclic dependency
foo() // doesn't work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM