So... there will be no consequences if it was left out?
Correct! Although omitting it does allow for situations like this:
JavaScript:(() => { console.log("IIFE 1"); })()
(() => { console.log("IIFE 2"); })()
Without semi-colons at the end of each IIFE, the engine has to guess where to put them. And in this case, it guesses wrong, yielding "intermediate value is not a function". Because it sees the second line as function arguments for the first line. With
void at the start of each line, no error occurs.
:kaohi:
I seem to recall instances where people wanted to change or add onto plugin functionality and trying to refer to/override the functions didn't work because they were wrapped in an IIFE so the code can't be externally referenced.
As-is, yes. For a "proper" plugin, you'd want to define any public data as:
A member of your plugin namespace - optional; a global variable/object on which you define everything public related to your plugin.
I just don't bother with that when it comes to small snipppets.
:kaoswt:
The global scope is still accessible from within the IIFE, and you can define new globals by referencing the global object explicitly (
window or
globalThis), e.g.
JavaScript:void (() => {
const $ = window.myNamespace || (window.myNamespace = {});
$.apple = "tasty";
})();
void (() => {
const alias = myNamespace.alias_Game_Player_realMoveSpeed
= Game_Player.prototype.realMoveSpeed;
Game_Player.prototype.realMoveSpeed = function() {
return alias.apply(this, arguments) - 1;
};
})();
console.log(myNamespace);
Edit: for my "full" plugins, I typically nest the namespaces: I have one global variable,
CAE, which has one property per plugin. Each of those properties serves as a namespace for the respective plugin.