JavaScript Before Modules - Immediately Invoked Function Expressions (IIFE)

preview_player
Показать описание
Before ES6 came out in 2015, JavaScript had not concept of modules and thus code outside of functions just became global. The immediately invoked function expression (or IIFE) was the solution to this.

Prepping for your frontend interviews? Use code "conner" for a discount on my course FrontendExpert:

Socials:
Рекомендации по теме
Комментарии
Автор

Now we just use it to add async in a place that’s not async

sobanya_
Автор

Use this a lot. Great explanation on it

walteredmond
Автор

Thanks this is very helpful for beginners, now i know what is the history of them...rather than using them two in the same time in my projects..

blackbeans
Автор

I did this once by mistake and changed my life

PostMeridianLyf
Автор

So that means if I have a doStuff variable holding a string, it won't be substituted if I use the function doStuff in a module written like this?

k_gold
Автор

Wow, I remember trying to learn IIFE's at bootcamp and wondering why the hell it was relevant...this makes sense. Javascript used to suck!

PraiseYeezus
Автор

You can also use the void operator before the function keyword then call it after the function block to do this, which I personally think is a bit cleaner. Good video!

nanoguy
Автор

But why you would like to do that ? It's to make the function scope only the file it's in?

alibadra
Автор

I truly hate JS lol. I'm an Ionic app I'm very happy with using it, but still every time there's some pain point.

evanbelcher
Автор

iffe works but how do you call the doStuff... you actually need to strore the main func in a variable as iffe and return the doStuff func and have smth like
let main = (function() { return doStuff() {} })()
main.doStuff() // this works now

Mr..Books
Автор

But what if we want to execute the function again
Wouldn't that create a problem since iife can be only executed once

davesharma
Автор

Looking at this makes me glad that I have not and never will use javascript.

NavroZ
Автор

gosh Javascript is a crufty language hahaha, what a weird syntax. It gets the job done I guess. Hoping for wasm first class DOM manipulation some time in the future so we can choose to not use JS at all.

minerscale
Автор

Sounds like JavaScript was unusable prior to 2015

jasonmurphy