What is an IIFE? | Javascript IIFE Function Expressions Explained

  Рет қаралды 21,766

Dave Gray

Dave Gray

Күн бұрын

Пікірлер: 72
@ichiroutakashima4503
@ichiroutakashima4503 Жыл бұрын
This channel needs more subscribers. The info is easy to digest. And your description and links you give are the holy grail needed by every beginner to learn.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Thank you!
@farruk_developer
@farruk_developer Жыл бұрын
@@DaveGrayTeachesCode for real you are a great guy
@srinivasaraoyp3640
@srinivasaraoyp3640 2 жыл бұрын
I am trying to learn IIFE, this video demo is simply the best. In the institutions, they only show one example. Here are so many examples. Thank you.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Glad it was helpful! 💯
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
IIFEs look unusual when you first see them. No worries! In this tutorial I break down what an IIFE is and how an IIFE is used in Javascript. This tutorial also references several other Javascript concepts including recursion, ternary statements, scope, and closure. I have links to tutorials about all of those concepts in the description of the video. If you are just starting to learn Javascript, I suggest starting with my full 8 hour Javascript tutorial found here: kzbin.info/www/bejne/e5eknWyYrN-JkM0 Note: There is a default parameter placement correction of the example at 1:30 noted in the description. Doh! Needed more ☕☕☕
@nikitak9628
@nikitak9628 3 жыл бұрын
Dave, this is a fantastic explanation of IFFEs. Very thorough, especially for going through the module and the revealing module pattern.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thank you, Nikita! 🙏
@hotkatgotama
@hotkatgotama 2 жыл бұрын
Dave, I just recently started watching your tutorials - superb content. Currently, I am doing a deep dive into ReactJs, NextJS and NodeJS (from a .NET background). I would love to see (in addition to your usual generic-type examples) some (at least one) real world examples, this might make your videos a bit longer - but well worth the effort. For example, these variations of the Module Pattern - I can see that a good use case for them would be when you only need one instance of an object - without inheritance, singleton code and all the other baggage. Would be great to see your response. Fantastic channel.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you, Katim! And thank you for the request / suggestions. 🙏 One instance of an object would be a singleton pattern if I am understanding your request correctly? Design patterns would be a great series to tackle! 🚀
@CodeWithTomi
@CodeWithTomi 3 жыл бұрын
Nice video... And congrats on 4k
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thanks Tomi! 🙏💯
@aman7555
@aman7555 3 жыл бұрын
Hello Dave, wonderful tutorial as always. A quick input, in the increment counter example, we don't need to pass counter to credits as it already has access to it because its in the lexical scope, [I understand scopes and closures better thanks to you] const increment = (()=>{ let counter=0; console.log(counter); const credits = () => console.log(`I have ${counter} credits`); return () => {counter++; credits();} })();
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
That is a good observation about lexical scope. If I didn't use the num parameter placeholder, it could have been written that way. 💯😀
@shobhitgarg6897
@shobhitgarg6897 Жыл бұрын
@@DaveGrayTeachesCode Hey , Great Tutorial !! Just one doubt, I have checked with normal functions they also have access to counter variable. So like whats the difference? const increment = ()=>{ let counter = 0; console.log(counter); const credit = (num)=>console.log(`I have ${num} credits`); return ()=>{counter++; credit(counter)} } const incrementFunction = increment(); console.log(incrementFunction); incrementFunction() incrementFunction() incrementFunction()
@B-Billy
@B-Billy 3 жыл бұрын
So much content in such a less time.. Bravo and subscribed !!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thank you! 🙏💯
@ranjankumar1324
@ranjankumar1324 3 жыл бұрын
Congratulations on 4k.., much more to come
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thank you! 🙏🚀
@ibrahim_youssef_13.12
@ibrahim_youssef_13.12 Жыл бұрын
I search about thing and open any video , then I sea Dave video , immediately close the video and open the Dave's video , YOU ARE AWESOME
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Thank you!
@KishoreRaju-s6u
@KishoreRaju-s6u 5 ай бұрын
Hi Dave, you are doing an amazing job, can you come up with more javascript advance concepts
@craigburton4447
@craigburton4447 2 жыл бұрын
Such a good tutorial Dave, several 'aha' moments for in it. Thanks!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Glad I could help! 🚀
@Miguel-Barcelo
@Miguel-Barcelo 2 жыл бұрын
Hi Dave, thanks for your service, you have my like on every video that I watch, really appreciate it
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you, Miguel! 🙏🙏
@shineLouisShine
@shineLouisShine 2 жыл бұрын
Fascinating concept! Although it does feels as if it requires a lot of practicing to be truly familiar with and apply it while coding with in high skillability. Thanks..
@ahmad-murery
@ahmad-murery 3 жыл бұрын
Injecting name space is useful to plugin more functionalities to the App as needed, for example we can create another separate IIFE to provides a decrement method to expand what the App can do Thanks Dave,
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Absolutely Ahmad. Glad to see your comment here! 🚀💯
@ahmad-murery
@ahmad-murery 3 жыл бұрын
@@DaveGrayTeachesCode Finally 😁, I thought I'll never be able to comment on your videos again, Thanks Dave and I appreciate your help
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
@@ahmad-murery agreed! For the context of anyone else reading this, we could not figure out why YT prevented his comments on my last video. Ahmad provides good comments, and I do not block them. Glad to have you back 🙏
@ahmad-murery
@ahmad-murery 3 жыл бұрын
@@DaveGrayTeachesCode So true, maybe YT has marked my comments as spam and auto deleted them because I always start them with "Hello Dave", so I didn't do it this time 😎
@emreozgun3846
@emreozgun3846 3 жыл бұрын
Informative as always Dave!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thanks! 🙏
@Panjax
@Panjax 6 ай бұрын
I'd have liked an explanation why these variations exist. Like the module pattern vs the revealing module pattern. I can see they're different but why? what problem does it solve being different like that. And therefore, why would we use it?
@chetanjain4616
@chetanjain4616 2 жыл бұрын
Love the series, I hoped there will be one for bind, call as well
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
I have not made those.. yet. Great requests! Thank you 🙏
@chetanjain4616
@chetanjain4616 2 жыл бұрын
@@DaveGrayTeachesCode I think video on those topics would complete the advance javascript series
@gordonmullen7272
@gordonmullen7272 3 жыл бұрын
Thanks again for a job well down: I see things I didn't before so THANK YOU !! hungry for more Mr. Grey, you're a Master Code Chef who plays a mean Van Halen lick. I used to be a Keyboard player in a band. Used an old Korg 800 + a whole rack. telling my age now lol. That was back when jump just hit the radio and I loved it.... anyway thanks so much!!!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thanks Gordon! 🙏 80s rock does have great guitar and keys! 🤘🤘💯
@darkhorsemusicco.5156
@darkhorsemusicco.5156 2 жыл бұрын
Excellent excellent video. Thank you
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You're welcome! 💯
@shawnjohnson3433
@shawnjohnson3433 Жыл бұрын
Very good teacher!
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Thank you! 😃
@ua2894
@ua2894 8 ай бұрын
What would be the use and benefit of injecting a namespace object?
@dumchik7290
@dumchik7290 Жыл бұрын
> function fun1(){return "hello"}() Uncaught SyntaxError: Unexpected token ')' > function fun2(x){return x*x}(5) 5 > 0,function fun3(x){return x*x}(5) 25 As expected in first case I get error as I didn't use paranthesis. But how the second case and third case get executed?
@dzienisz
@dzienisz 11 ай бұрын
Where would you use IIFE in real case application? Any particular module?
@delyanmitev7728
@delyanmitev7728 Жыл бұрын
Thanks my dude. 1st result when I googled and I never never got to the 2nd one.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Right on! 🤘
@jingli9232
@jingli9232 2 жыл бұрын
thx for sharing you knowledge
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You're welcome!
@jiweihe3413
@jiweihe3413 2 жыл бұрын
A bit confused about the last example "injecting a namespace object" at the end of the video. Why the input to the function is not "namespace=XXX" but "window.App=window.App" instead?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
In the example, the window object is being passed in and we are creating a namespace in the property App. At 13:50 I explain that it much like defining const App = { } ...those are the arguments passed in when the function is called. In the example, the word "namespace" is a parameter placeholder in the function definition.
@jiweihe3413
@jiweihe3413 2 жыл бұрын
@@DaveGrayTeachesCode thank you. Just realized namespace is not a random object name.
@tularamsa5814
@tularamsa5814 2 жыл бұрын
awesome!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@jiweihe3413
@jiweihe3413 2 жыл бұрын
10:30 module pattern looks a bit like factory function but is immediately invoked. Is there any relation between the two?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
They are similar!
@shanemarchan658
@shanemarchan658 3 жыл бұрын
and that's why bananas have no toes.. if u need a tutorial to that, ive got a link in the description down below
@JoeeBiden-j1t
@JoeeBiden-j1t Жыл бұрын
myIffy() function has no parameters. But while invoking a parameter is used (num =0). How is it? Could you please explain it?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Sounds like a default parameter: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters
@JoeeBiden-j1t
@JoeeBiden-j1t Жыл бұрын
@@DaveGrayTeachesCode , thanks for your reply. I'm learning a lot from your videos.
@st10689
@st10689 Жыл бұрын
Hi my question is why it is necessary to wrap the whole function definition inside parenthesis and then call it. I mean why this does not work const test = ( ) => { console.log(10) } ( ); Why it needs the added parenthesis, so basically be like that to work const test = (( ) => { console.log(10) }) ( ); OR THIS ( ) => {console.log("Hello") ( ); -> Not working (( ) => {console.log("Hello")) ( ); -> Working
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Good question! It needs to be grouped together so the operators () are calling the complete function. For another example, if we have: 'Dave'+'Gray'.toUpperCase() ...what do you think will be uppercase? Only Gray because toUpperCase() is only called on the last string. However: ('Dave'+'Gray').toUpperCase() makes both Dave and Gray uppercase.
@ravivarma7837
@ravivarma7837 2 жыл бұрын
every function create its own scope so the variables or functions inside the function would only be created in the same function scope and it would not populate global scope i am very confuse as the below code does the same as IIFE const x = "whatever global Scope"; const helloworld = () => "Hello World from global Scope"; function foo(){ const x = "whatever foo Scope"; const helloworld = () => "Hello World from Foo Scope"; console.log(x); console.log(helloworld()); } foo(); console.log(x); console.log(helloworld());
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
In your example, you have 3 globals: x, helloworld, and foo. You could wrap all of those in an IIFE and have no globals. You are correct that each function creates its own scope. This is also true of an IIFE function. All of the other functions you create would be within the scope of the IIFE and not in the global scope.
@ravivarma7837
@ravivarma7837 2 жыл бұрын
after months of wrapping my head around finally I understood thanks alot ​ @Dave
@abdulrehman-rt5mz
@abdulrehman-rt5mz Жыл бұрын
@@ravivarma7837 same confusion going with me, can u pls what did u conclude ?
@Really_riel
@Really_riel Жыл бұрын
When I entered the code at 3:03 . I keep getting a reference error saying “nun is not defined”. Please I’m confused.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
There is a correction that pops-up at 1:30 and is noted in the description. That should help!
@yabuking84
@yabuking84 5 ай бұрын
isnt it only VAR can be global variables?
@jcubic
@jcubic 3 жыл бұрын
I'm afraid you have a logic error in your code })(num = 0); will create a global variable because it's not a parameter just an expression. You want to put this in function argument not when you call it. I would add a banner to correct that. Also you're using IFFE it isolate the scope but with const and let you're using you can just curly braces you don't need IFFE anymore in ES6 and beyond.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thanks for the note. 🙏 I did not consider the default parameter placement correctly when the IIFE is called. Note will be added. Update: It seems YT no longer allows video annotations, but I have added a note in the description. Concerning your 2nd comment on not needing IIFE after ES6 and beyond, I state in the video this is intended to identify what you may see in legacy code. Legacy code will likely use var, but I can't make myself go back lol. I agree that curly braces create a local block scope and you can verify in my scope tutorial: kzbin.info/www/bejne/lXacZ4psntt7pMk Thanks again for the comment! 😀
Javascript Closure Tutorial | Closures Explained
17:58
Dave Gray
Рет қаралды 150 М.
The different types of JavaScript functions explained
14:47
Kevin Powell
Рет қаралды 42 М.
My Daughter's Dumplings Are Filled With Coins #funny #cute #comedy
00:18
Funny daughter's daily life
Рет қаралды 29 МЛН
Зу-зу Күлпаш 2. Интернет мошенник
40:13
ASTANATV Movie
Рет қаралды 540 М.
Epic Reflex Game vs MrBeast Crew 🙈😱
00:32
Celine Dept
Рет қаралды 39 МЛН
Shallow Copy and Deep Copy | Advanced Javascript Tutorial
26:36
What is __proto__ ? | Javascript Prototypes Tutorial
23:11
Dave Gray
Рет қаралды 44 М.
Test-Driven Development // Fun TDD Introduction with JavaScript
12:55
JavaScript Closures Tutorial (Explained in depth)
19:03
ColorCode
Рет қаралды 72 М.
10 Javascript Functions Examples You Can Use to Save Time
19:15
JavaScript IIFE (Immediately Invoked Function Expressions)
5:58
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 8 М.
Inheritance in JavaScript - Prototypal Inheritance tutorial
20:06
My Daughter's Dumplings Are Filled With Coins #funny #cute #comedy
00:18
Funny daughter's daily life
Рет қаралды 29 МЛН