Animations
API
Arrays
Async
Basics
Challenges
Classes
Console
Dates
Debugging
DOM Elements
DOM Methods
DOM Properties
Event Listeners
Flow Control
Forms
Functions
Global Functions
JSON
Keywords
Libraries (3rd party)
Math
Modules
Objects
Snippets
String
Types
Widgets
Window Object
An IIFE
function is a self-invoking function. See Self Invoking Functions.
You may commonly see or use code like this: self-invoking-function-example.js.
A self-invoking expression is invoked (started) automatically, without being called. You have to add parentheses around the function to indicate that it is a function expression.
Notice that we do not have access to _defaultMsg
. It is protected by the anonymous functions scope.
(function ()
{
"use strict";
let _msg = null;
let _defaultMsg = "Hello World";
console.log("SelfInvokingExample invoked");
const Initialize = (msg) =>
{
_msg = msg;
};
const GreetingAlert = () =>
{
alert(_msg);
};
const GreetingConsole = () =>
{
console.log(`Greeting Msg: ${_msg}.`);
};
return {
Initialize: Initialize,
GreetingAlert: GreetingAlert,
GreetingConsole: GreetingConsole
}
})();