Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does import * do in Javascript?

I was browsing through this repo on Github and was trying to comprehend the working of the code

Here, the author (or programmer) have mentioned import * at multiple places so I am trying to comprehend and understand how import * work?

First in Game.js file of his repo he have mentioned/written like this

import * as actions from '../actions';

In VS Code, when if I click on '../actions using command It is redirecting me to this file -> index.js

then in Index.js they have something like this

import * as ActionTypes from './action-types';

when I click on ./action-types it redirects me to here action-types.js

I went through firefox docs but I wasn't able to clearly make sense for the first example like for one, the action folder contains multiple files and how does import * as actions from '../actions'; mean index.js file

While i get he have called/referenced the functions using actions.functionName() or ActionType.TypeName

My Prime question remains

how does import * as actions from '../actions'; mean index.js file ?

like image 616
iRohitBhatia Avatar asked Nov 15 '18 13:11

iRohitBhatia


People also ask

What does import * mean in JS?

The import() call, commonly called dynamic import, is a function-like expression that allows loading an ECMAScript module asynchronously and dynamically into a potentially non-module environment.

What is import * In react JS?

Importing allows using contents from another file, whereas exporting makes the file contents eligible for importing. The basic idea behind imports and exports is to exchange contents between several JavaScript files. Let's look at these features one by one.

What does export * Do JavaScript?

The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the import declaration or dynamic import.

What does it mean import * in Python?

In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.


2 Answers

The import * as name syntax imports all exported content of a javascript file.

For example, if you want to import an entire module's contents, then access the doAllTheAmazingThings() function

import * as myModule from '/modules/my-module.js';
myModule.doAllTheAmazingThings();

From the docs

like image 83
Gökhan Musapaşaoğlu ヅ Avatar answered Oct 27 '22 00:10

Gökhan Musapaşaoğlu ヅ


Import in js is new syntax of ES6 to import a module it has the same work of require but its easier to filter what do you want in a module

In your example you import * as actions from '../actions'; you import all function from ../actions file

its same to do const actions = require('../actions')

but its easier to manage what you want

this syntax is not work on all browser so be sure to use transpiler with babel or other

you can see this syntax in python too

like image 33
InitialCrow Avatar answered Oct 27 '22 01:10

InitialCrow