developerasun / myCodeBox-web

Open source code box for web developers.
Apache License 2.0
5 stars 0 forks source link

[RESEARCH] Javascript/module: side effect #270

Open developerasun opened 2 years ago

developerasun commented 2 years ago

topic : understanding side effect in Javascript module

read this

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.

reference