Тёмный
No video :(

What is an IIFE? | Javascript IIFE Function Expressions Explained 

Dave Gray
Подписаться 333 тыс.
Просмотров 21 тыс.
50% 1

Опубликовано:

 

27 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 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
@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: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-EfAl9bwzVZk.html Note: There is a default parameter placement correction of the example at 1:30 noted in the description. Doh! Needed more ☕☕☕
@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! 💯
@nikitak9628
@nikitak9628 2 года назад
Dave, this is a fantastic explanation of IFFEs. Very thorough, especially for going through the module and the revealing module pattern.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 года назад
Thank you, Nikita! 🙏
@B-Billy
@B-Billy 2 года назад
So much content in such a less time.. Bravo and subscribed !!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 года назад
Thank you! 🙏💯
@CodeWithTomi
@CodeWithTomi 3 года назад
Nice video... And congrats on 4k
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 года назад
Thanks Tomi! 🙏💯
@ranjankumar1324
@ranjankumar1324 3 года назад
Congratulations on 4k.., much more to come
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 года назад
Thank you! 🙏🚀
@ibrahimyoussef4489
@ibrahimyoussef4489 Год назад
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!
@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! 🚀
@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()
@user-qg5ow7of4e
@user-qg5ow7of4e 3 месяца назад
Hi Dave, you are doing an amazing job, can you come up with more javascript advance concepts
@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! 🙏🙏
@craigburton4447
@craigburton4447 2 года назад
Such a good tutorial Dave, several 'aha' moments for in it. Thanks!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 года назад
Glad I could help! 🚀
@shawnjohnson3433
@shawnjohnson3433 Год назад
Very good teacher!
@DaveGrayTeachesCode
@DaveGrayTeachesCode Год назад
Thank you! 😃
@emreozgun3846
@emreozgun3846 3 года назад
Informative as always Dave!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 года назад
Thanks! 🙏
@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
@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..
@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! 🤘🤘💯
@jingli9232
@jingli9232 Год назад
thx for sharing you knowledge
@DaveGrayTeachesCode
@DaveGrayTeachesCode Год назад
You're welcome!
@Panjax
@Panjax 4 месяца назад
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?
@ua2894
@ua2894 6 месяцев назад
What would be the use and benefit of injecting a namespace object?
@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 😎
@darkhorsemusicco.5156
@darkhorsemusicco.5156 2 года назад
Excellent excellent video. Thank you
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 года назад
You're welcome! 💯
@dzienisz
@dzienisz 10 месяцев назад
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! 🤘
@dumchik7290
@dumchik7290 11 месяцев назад
> 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?
@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.
@yabuking84
@yabuking84 3 месяца назад
isnt it only VAR can be global variables?
@tularamsa5814
@tularamsa5814 2 года назад
awesome!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@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
@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!
@user-jw5op8sl6n
@user-jw5op8sl6n Год назад
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
@user-jw5op8sl6n
@user-jw5op8sl6n Год назад
@@DaveGrayTeachesCode , thanks for your reply. I'm learning a lot from your videos.
@ravivarma7837
@ravivarma7837 Год назад
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 Год назад
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 Год назад
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 ?
@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.
@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!
@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: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-_E96W6ivHng.html Thanks again for the comment! 😀
Далее
What is Factory Function in JavaScript? - JS Tutorial
17:31
Only I get to bully my sister 😤
00:27
Просмотров 28 млн
The different types of JavaScript functions explained
14:47
What is __proto__ ? | Javascript Prototypes Tutorial
23:11
WTF Do These Even Mean
13:44
Просмотров 84 тыс.
How I Made JavaScript BLAZINGLY FAST
10:10
Просмотров 217 тыс.
JavaScript Closures Tutorial (Explained in depth)
19:03
Learn Regular Expressions In 20 Minutes
20:52
Просмотров 1,3 млн