Skip to content

Closure & IIFE

A. CLOSURES

  • intro:
  • A closure is a fundamental concept in JavaScript that allows functions to "remember" and access their lexical scope even when they're executed outside that scope
  • Function which returns anothor function
    img
  • get retuned function from closure img
  • invoke retuned function img
  • 3 steps: closure maintains the state of return functions.

  • run the closure with arg1 and get retuned function1 from closure.

  • Run the closure again with agr2 and get another retuned function2.
  • run the retuned function1. img

B.IIFEs - Immendiately invoked Function Execution

  • which function get executed Immendiately.
//1. Normal Function
function(){     console.log("hello");  }

//2. IIFEs
( function(){    console.log("hello");  } )();

//3. IIFEs with Arg
( function(arg){     console.log(arg);  } )("hello2");
  • IIEFs have there own local scope: img

  • using global scoped obj in IIEFs scope: img


C. Extra : Built-in mehods and properties

1. arguments

img img

  • technically arguments are object [check above output {}], but act as array.
console.log(arguments)

console.log(arguments[0])
console.log(arguments[3]) // undefined

console.log(arguments.length) //2

2. function name and arg length

msg.name //message whuich is fn name
msg.length // 2

img

  • name of anonymous function img

cheatsheet