Help making plugin

Hello.
I'm developing a plugin and would like to know the best approach:

Make a single "(function () {"
and put all the contents of the plugin.

OR

Make several "(function () {"
for each part of the plugin.

?
It's just a matter of style i think. As long as you feel comfortable about it ...
Or neither.
The reason to create an IIFE is to locally scope your variables to avoid conflicts with variables that might share the same name. Alternatively, you could set up a global namespace and perform all of your actions within that namespace (see Yanfly plugins for an example). Both approaches are viable, whatever is most readable and maintainable for you should be the best solution.
IIFEs can make using a single namespace easier to maintain, and can also be used, even in strict mode, for selective overrides. Example:
Code:
// import declarations for ease of compatibility checks
var Imported = Imported || {};
Imported.plugin = true;

// namespaces for organisation
var author = author || {};
author.plugin = author.plugin || {};

// define stuff on namespace via IIFE parameter
(function($) {
'use strict';  // IIFE confines strict-mode declaration
  $.foo = 123;  // shorter to type out
  $.bar = 'text';
})(author.plugin);  // easy to alter later if needed

console.log(author.plugin.foo);  // proof of scope/persistence

// sub-routines with early returns, e.g. compatibility patches
(function() { if (!Imported.YEP_CoreEngine) return;
  // do this only if using Yanfly's Core Engine
})();

console.log(author.plugin.bar);  // regardless of whether using YEP_CoreEngine
(function($) {
'use strict'; // IIFE confines strict-mode declaration
$.foo = 123; // shorter to type out
$.bar = 'text';
})(author.plugin);
What does it mean to include author.plugin inside the parenthesis at the end of this code?
What does it mean to include author.plugin inside the parenthesis at the end of this code?
It's simply an input for the function. 🙂 With a "normal" function, the function is defined and then called, e.g.
Code:
var sayThis = function(text) { console.log(text); };
var input = 'Hello world!';
sayThis(input);
By contrast, an IIFE defines the function in-line and evaluates it immediately because it's in brackets. It still takes input(s) from brackets after the function:
Code:
var input = 'Hello world!'
(function(text) { console.log(text); })(input);
Since author.plugin was defined at the start (you can name them whatever you like within reason, they're just variables) this means we can pass it in as an argument of the function and refer to it by another name while inside that function. So, in this example, whenever you see $ inside that IIFE, it represents author.plugin. It's nice not needing to use the full name all the time, and definitely reduces the chance of making a typo~!
Is it possible to assign IIFE to a variable for later calls? Like this:
var input = 'Hello world!'
var ABC = (function(text) { console.log(text); })(input);
ABC("123");
Is it possible to assign IIFE to a variable for later calls? Like this:
As far as I know, an IIFE's purpose is to be called only once. That's why it is called Immediately Invoked Function Expression. So you declare and invoke it at the same time.
If you want to store it for later calls, you don't need an IIFE. Just assign a regular function to a variable, like this:
Code:
ABC = function(text) { console.log(text) };
ABC("123")
WOW... This is too "hardcore" for me yet xD
But i will keep learning!! 😀

Original thread: https://forums.rpgmakerweb.com/threads/help-making-plugin.106624/