Monday, May 20, 2024
3
rated 0 times [  5] [ 2]  / answers: 1 / hits: 24453  / 7 Years ago, tue, august 15, 2017, 12:00:00

Is there any difference between:



import utils from 'utils'



and



import * as utils from 'utils'?



In case A:



//utils.js
export function doSomething()
{
//...
}


In case B:



//utils.js
export function doSomething()
{
//...
}
export default function doSomethingDefault()
{
//...
}


UPDATE:



I was mislead by intellisense feature of vscode, but as recommended a small test on node+babel showed the difference:



//index.js
import utilsCaseA from './utils1'
import * as utilsCaseAWildcard from './utils1'
var utilsCaseARequire = require('./utils1')

import utilsCaseB from './utils2'
import * as utilsCaseBWildcard from './utils2'
var utilsCaseBRequire = require('./utils2')

var compareObjects =
{
utilsCaseA, utilsCaseAWildcard, utilsCaseARequire,utilsCaseB,utilsCaseBWildcard,utilsCaseBRequire
};
console.log(compareObjects);


enter


More From » ecmascript-6

 Answers
17

From your example:



Case A:



//utils.js
export function doSomething()
{
//...
}


Case B:



//utils.js
export function doSomething()
{
//...
}
export default function doSomethingDefault()
{
//...
}


Result:



import utils from 'utils'
utils() // (Case A: undefined, Case B: doSomethingDefault)

import * as utils from 'utils'
utils // (Case A: utils = { doSomething: function }, Case B: utils = { doSomething: function, default: function })

import { doSomething } from 'utils'
doSomething() // (both Case A and Case B: doSomething = doSomething)


The difference between Case A and Case B is that, in Case A import utils from 'utils', utils will be undefined because there is no default export. In case B, utils will refer to doSomethingDefault.



With import * as utils from 'utils', in Case A utils will have one method (doSomething), while in Case B utils will have two methods (doSomething and default).


[#56754] Sunday, August 13, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
travion

Total Points: 137
Total Questions: 96
Total Answers: 103

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
travion questions
Mon, Dec 16, 19, 00:00, 5 Years ago
Sat, Oct 19, 19, 00:00, 5 Years ago
Fri, Sep 20, 19, 00:00, 5 Years ago
Wed, Nov 14, 18, 00:00, 6 Years ago
Sun, Oct 28, 18, 00:00, 6 Years ago
;