Immediately Invoked Functions


English (Auto-generated)

hi everyone in this cast, we'll talk about immediately invoked function expressions which are extremely useful now. Usually a function is executed after it is called. However, you can also execute functions immediately after defining it and you can do this with immediately invoked function expressions. These do not require a function call And important about ice as I'll call them, which I mean are immediately invoked. Function expressions is the syntax so pay attention to the syntax and I'm just going to type something here and I'm going to say um let me open up the console so you can see this. So I'll say um, function and then there's going to be some function logic and out here I'll just say console dot log run immediately and once I'm done, I'll end within opening and closing pair of brackets. Right? So out here in the console you can see the statement run immediately. Now looking carefully at this syntax, it's noticeable that the function statement is enclosed within a parenthesis, so an an opening and closing a pair of parenthesis and then immediately this function is called by appending a pair of parenthesis, which is this pair at the end. So the first enclosing parenthesis make the function and expression and the last set of parenthesis will immediately signal the execution of the function. Court statements within an knife are within their own lexical scope so any variables that are enclosed within this life cannot be accessed outside of it. So from this example we can see that there are two steps to creating an AIF. So first of all you have to create a function expression and this is denoted by the opening and closing pair of parenthesis. So within this pair of parenthesis you're going to basically enclose your function and then you invoke this function expression immediately with the closing pair of a parenthesis. And you'll see that the function is invoked immediately and this statement is logged to the consul. Now the return value of an Aif is an execute herbal function. So you can also have a named immediately invoked function expression. So this could just be function life like that now named and unnamed immediately invoked function expressions do not leak into the global space and cannot be called again and moving on, you can also pass in parameters to an eye. So for example, out here I'll say function takes as a parameter A and B. And what is going to happen out here is I'm going to consul a dot log B plus b and within the second pair of parenthesis I'll actually pass in some arguments in place of A and B. So 100 and 1500 and there you go. So I get my some and I also get this statement which is run immediately now why are useful is because a lot of javascript libraries will use immediately invoked function expressions so as to avoid naming conflicts within the global name space between the library and the programs that use that specific library. So for example, let's just analyze some more code and out here let me actually just comment this out and let me clear this console. Okay, right. So out here I have a global variable. So let number one is equal to the number 10 and then I'm going to have a knife so my opening pair of parenthesis and then my immediately invoked function expression afterwards and out here what I'm going to do is I'll have a local variable. Let number one is equal to function uh sorry, let number one is equal 200 then I will cancel a dot log out here numb one. Right. And now let's not forget that I need my clothing pair of parenthesis immediately after to invoke this fun um function expression immediately right now. Within the life the number one variable will be locked to the console immediately when this life is executed. Whereas outside of the IIhF if I console dot log a numb one we'll see that the value of number one in the global name space, which is 10 will be logged to the console and this is because javascript has function level scope so the variables that are in the function right in this case the aif are local variables and cannot be accessed outside of the function and outside the function, the value of number one which is a 10 and is within the global name space is logged to the console and we can also achieve the same result by actually using the let keyword and if you remember let his block scoped. So let me just call this comment this out and let's uh do this By using lead. So out here I have led, number one is equal to 10 and then I have an opening and closing pair of parenthesis which means this is uh some block level scope, so let number one out here is 100. And if I console dot log number one, I'm going to see the number 100 outside the pair of apprentices. If I console dot log number one in this case you will see the number 10. So 100 is logged first which is the value of number one within this block and then within the global name space, the value of number one is 10 and that is logged here. So the advantage of using a knife is that the function executes immediately. Therefore, if that's what you need in a web application, this construct is available. Also any variables declared inside an aIF cannot be accessed outside. And this is going to prevent polluting the global name space and avoid naming conflicts and additionally, immediately invoked functions also help free up memory. This is because as soon as an ice is invoked, the variables and life itself are available for garbage collection. Therefore freeing up memory. So that's all about life for this cast
75 Views 0 Likes 0 Comments

A brief primer to IIFEs which as functions that are immediately invoked and do not require a function call

Leave a comment (supports markdown format)