Using Immediately Invoked Function Expressions (IIFEs)

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ก.ค. 2024
  • Immediately Invoked Function Expressions or IIFEs are a staple of JavaScript development, but can sometimes be confusing to the uninitiated. In this tutorial we will cover IIFEs so that anyone can understand them.
    For more resources on JavaScript:
    www.allthingsjavascript.com
    Full personalized courses on JavaScript:
    allthingsjavascript.com/person...
    Tutorials referred to in this video:
    Function declaration VS function expression: • Function Declarations ...

ความคิดเห็น • 27

  • @JavierCarrion
    @JavierCarrion 6 ปีที่แล้ว +6

    Thank you for clarifying the IIFE construct as also for giving the real world scenario on how you can use it. Great video!

  • @rakibul1765
    @rakibul1765 4 ปีที่แล้ว

    you are a great teacher. I follow you for long

  • @leeroy77
    @leeroy77 3 ปีที่แล้ว

    Great explanation. Nice and clear.

  • @iliyanlekov2172
    @iliyanlekov2172 4 ปีที่แล้ว

    Wow....perfect video!

  • @seenuvasanv
    @seenuvasanv 6 ปีที่แล้ว

    Thanks.

  • @ChrisTian-ox5nr
    @ChrisTian-ox5nr 3 ปีที่แล้ว +1

    Douglas Crockford
    said when you invoke an IFFE like so: (function(){
    console.log(5 * 5);
    })(); that it looks like a pair of dog balls hanging lol that a more intuitive way would be to wrap it in parenthesis. (function(){...}()); But anywho great tutorial !

  • @faizanrahman9141
    @faizanrahman9141 6 ปีที่แล้ว

    Hello,
    Does the advanced JS course on udemy cover things that would help me in backend JS programming, specifically Node.js, so things like callbacks, closures, and other advanced stuff?
    Thanks for the TH-cam videos.

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 ปีที่แล้ว

      Node is not covered in the Advanced JS course. I hope to make a separate class on node.

  • @m12652
    @m12652 4 ปีที่แล้ว

    Cool, but... I get the workings but given a script doesn’t need the code to be wrapped in functions anyway, I.e. you could just have “console.log(5*5);” in a script file all by itself and it would execute every time the page loads (I.e. immediately) what’s the advantage of wrapping it in “function” etc.?
    Very probably a dull question ;o)

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      One advantage of wrapping something in an IIFE is that now you have a function enclosure. Because of the nature of closure in JavaScript this is very powerful. You can do all sorts of things inside the IIFE without polluting the global space. This might help explain that concept: th-cam.com/video/SMUHorBkVsY/w-d-xo.html

  • @ilhomhuja
    @ilhomhuja 6 ปีที่แล้ว

    can u also please do some tutorials on ajax-pure javascript-json-php load data while scrolling down, there tons of tutorials useing mysql and jscroll or kqueries, can we do with simply pure javascript without any of those jquery or jscrolls, please, pure clean javascript. i know we need ajax,php and json as well,

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 ปีที่แล้ว

      There are some tutorials on JSON. I'm working on putting together more tutorial on asynchronous JavaScript, which would include the ajax stuff. I'm planning to do those as I work on an Asynch course.

    • @easternhair
      @easternhair 6 ปีที่แล้ว

      yeahhhhh very excited then, thanks much waiting waiting fruther tutorials

  • @asmartbajan
    @asmartbajan 6 ปีที่แล้ว +1

    6:44 Most code in the wild has the function expression followed by the invocation operator (like you prefer), but Douglas Crockford begs to differ: th-cam.com/video/eGArABpLy0k/w-d-xo.html&t=1m10s

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 ปีที่แล้ว +1

      I think most do it with the invocation operator outside because it matches regular function invocation. Here is my thinking: When you invoke a function you put the invocation after the function name. Well, an IIFE is just a function, we just aren't referring to it with a name. Therefore, I prefer to define the IIFE first (this includes the parens) then invoke. That means the invocation operator is outside the parens.

  • @valikonen
    @valikonen 5 ปีที่แล้ว

    Unfortunatly very seldom find IIF with params...

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว +1

      If you use a module pattern, you will frequently have parameters, but outside of that maybe not so much.

    • @valikonen
      @valikonen 5 ปีที่แล้ว

      For interviews is a classical question "how to get values from a `for` and timeout" and you can solve it with iff with params like:
      for(var i = 0; i

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว +1

      Thanks for sharing!

    • @agnichatian
      @agnichatian 5 ปีที่แล้ว

      Carnaru Valentin - Are you sure about that ? setTimeout is not getting a callback function , but a value. Also, if you put 1000*i for the time it'll execute in sequence - as shown it spits out all answers at once with no delay on them.

  • @jkovert
    @jkovert 5 ปีที่แล้ว

    Lose Mr Neckbeard on your website.

  • @advertisingsea559
    @advertisingsea559 5 ปีที่แล้ว

    Here is wrong url adress prntscr.com/kwnrmn

  • @DipenduDasDip
    @DipenduDasDip 4 ปีที่แล้ว

    The concepts are not exactly same what is shown in the video. So It's not a good guide.

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      Can you be more specific? What do you have a concern with?