Skip to content

Latest commit

 

History

History
57 lines (48 loc) · 887 Bytes

File metadata and controls

57 lines (48 loc) · 887 Bytes

IIFE Immidiately invoked function

  • IIFE - Immidiately Invoked Function Expression
  • Once the file is loaded, IIFE invokes- there is no need to call function name bracket().
function test(){
    console.log('hello');    
}
test(); //<--invoked 

test.js

  • Once test.js is loaded, below function is invoked.
(function (){
    console.log('hello');    
})(); //<-- Invoked

Example

main.js

"use strict";
(function () {
    var thing = {"hello": "main"};
    console.log(thing);
})();

other.js

"use strict";
(function () {
    var thing = {"hello": "other"};
    console.log(thing);
})();

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <script src="main.js"></script>
    <script src="other.js"></script>
</body>
</html>

Result of executing index.html

{hello: "main"}
{hello: "other"}