I was reading the es6 import statement reference on MDN. The syntax:
import "my-module";
will import an entire module for side effects only, without importing any bindings. I am not sure what side effects mean. I have been using with angular by saying import "angular"
. Angular binds to window but does not return a object. So I am not sure if this would be called as a side effect exactly.
A module with side-effects is one that changes the scope in other ways then returning something, and it's effects are not always predictable, and can be affected by outside forces (non pure function).
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.
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.
Javascript import statement is used to import bindings that are exported by another module. Using import, the code is easier to manage when it is small and bite-size chunks. This is the thinking behind keeping functions to only one task or having files contain only a few or one component at a time.
When you need to import something that doesn't export anything, but does something else, this is a side effect only module. You import it only to initialize it.
Pure and Non Pure Modules
If you think about modules as functions, a module that only effects the scope by exporting it's content is like a function that always returns the same thing (a pure function without parameters). No matter how many times you'll import react 15.01, you'll always get an object that contains the same methods.
A module with side-effects is one that changes the scope in other ways then returning something, and it's effects are not always predictable, and can be affected by outside forces (non pure function). A polyfill for example, might not do anything, because it finds that the feature that it enables is already supported by the browser.
Examples of side effects:
window
object, but doesn't export anything.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With