Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Qualified and unqualified imports work the same way (they are both indirections)

What is the distinction and therefore what does this statement mean?

like image 433
Arijit Bhattacharya Avatar asked Jan 22 '16 18:01

Arijit Bhattacharya


People also ask

What is a qualified import?

A qualified import allows using functions with the same name imported from several modules, e.g. map from the Prelude and map from Data.

What is ES6 import?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

Does ES6 support import?

Introduction to ES6 import:The import statement is used to import modules that are exported by some other module. A module is a file that contains a piece of reusable code. The import modules are in strict mode whether it is declared or not.

What are imports in JavaScript?

The import statement makes modular code possible in JavaScript (and also, code reuse and sharing). Without import or a similar mechanism, a developer could only write a JavaScript program as a single program in one file.


1 Answers

Strictly speaking, there's no such thing as qualified/unqualified imports in JavaScrpit. Those terms were used in the book 'Exploring ES6' by Dr. Axel Rauschmayer in the context of cyclic dependencies, and roughly mean:

Unqualified imports (directly import a part of a module):

CommonJS:

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

ES2015:

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

Qualified imports (import the whole module as a namespace):

CommonJS:

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

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:

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
like image 125
marzelin Avatar answered Sep 20 '22 05:09

marzelin