I have created a particular effect and wrapped it into a self-invoking function in myEffect.js file,
(function () {
// yada yada...
}());
Is it possible to then use the es6 way of importing to import this into my main file so that it will run as it is? Reason I'm doing this is my main js file has other misc stuff and this effect is pretty long in itself and I'm hoping to be able to split things up.
Self-Invoking function is a type of function that is invoked or called automatically after its definition when followed by the parentheses set () and primarily used for the initialization tasks.
The import/export syntax is called JavaScript modules. In order to be able to import a function from a different file, it has to be exported using a named or default export.
HTML imports use the <link> element to reference the file that you wish to import; this works in a similar way to how you include stylesheets. Make sure that you set the rel attribute to import to indicate to the browser that the referenced file should be imported into the document.
A self-invoking (also called self-executing) function is a nameless (anonymous) function that is invoked immediately after its definition. An anonymous function is enclosed inside a set of parentheses followed by another set of parentheses () , which does the execution. (function(){ console. log(Math.
The effect will run when the module is evaluated, which happens when it is imported at least once in some other module.
You don't need an IIFE at all, ES6 modules already provide their own scope.
You don't need to export anything, as all your module is supposed to do is execute the side effect. It does not have a result value. (Which could be considered a design flaw, but let's not discuss that).
All you need to do is
// myEffect.js
… // yada yada
// main.js
import 'myEffect.js';
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