Understanding import syntax in JavaScript

3366784 :

Importing functions from another file in javascript

import { myFunction1, myFunction2 } from './myFileName';

Hi, I'm confused by the dictionary { } like syntax here.

Questions are:

  1. is { myFunction1, myFunction2 } a dictionary/map?

I then experimented with the syntax, I tried to create a dictionary/map with functions in them. It turns out that it works. It seems like a function variable name is turned into a key and the function is the value.

let funcDic = {
    foo, bar
}
function foo() { console.log('foo') } 
function bar() { console.log('bar')}
funcDic['foo']() // prints foo
Josh Wulf :

No it is ES6 object destructuring.

const record = { user: { name: 'Joe Bloggs' } }

const { user } = record

// user is { name: 'Joe Bloggs' }

const { name } = user

// name is 'Joe Bloggs'

// shorthand for:

const user = record.user
const name = user.name

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=30083&siteId=1
Recommended