DEV Community

loading...

Discussion on: Explain IIFE(Immediately Invoke Function Expression) Like I'm Five

Collapse
shiling profile image
Shi Ling

Analogy:

IIFE
Ask friend for the WIFI password, use it immediately, forget about the password afterward.

Normal Function
Ask friend for the WIFI password, writes it on a piece of paper, paste on the wall, everyone knows it.

What's the point of IIFE?

The IIFE is a javascript design pattern invented in the days before modules was introduced in ES6, and people wanted to run a piece of code only once, like a setup code, without polluting the global namespace.

e.g.

(function(){

    // some code you just want to run only once, e.g. setup Vue js.
    var app = new Vue({
        el: "#main",
        //... more stuff
    })
})()
Collapse
latheresienne profile image
Latheresienne Author

Thanks for the explanation😃