In simple words, if we try to understand, “undefined” is the absence of a value. It is one of the primitive data types. Show As per the MDN definition,
A variable that has not been assigned a value is of type Let’s try to understand by a simple example. The output for the above program will be as follows.
Let’s try to understand the code in a step-wise manner. If you see, the “unleashThis” is an object literal which has two properties and a function. Let’s see how the JS engine will execute the above code. The first line of the code is the variable that gets declared and not assigned at this point in time. Note: Please don’t get confused with the “this”. We are yet to arrive at the conclusion point. Moving on… On the Call stack, in the global Execution Context, the “unleashThis” is declared. Remember one thing, if you try to assign the function to a variable, it will put the entire function on the call stack. Note: The variable when put on the call stack, gets undefined but in case of the entire function gets put on the call stack. if there is no return value from a function, it returns undefined. Line 11 will be put on the call stack where we are invoking the loopItNow. Now, here comes the twist.
The moment when the execution engine reaches line 5, what should be the “this” value? 🤔 Currently, a new Execution context has been created where the scope is local to the object. The reason is that you have to see from where the “this” keyword has been invoked. “this.friends” belongs to the local scope of the unleashThis object. So, the value of friends array will be placed in it. Now, the next line of execution will be Line 6 where we are trying to print the value to the console. Remember, the moment the JS engine sees the “()”, it gets excited to create a new Execution Context. If you try to visualize the JS engine execution code. Let’s see the diagram below. So, in line 6, when “this” tries to find the “name” in the scope of the window, it doesn’t find it and returns undefined. Now, the next question will be, what could be the probable solution for the above problem.
Let’s try to understand every solution one by one.
This “var self=this;” is one of the most used statements to handle “this” context problem even in the large enterprise application. What do you think this statement is doing? Actually, it will be interesting to understand that the moment when line 6 starts executing, a “Closure” on the loopItNow is created that holds the “self” object. The self object has now the same properties and function that the unleashThis object contains. But, the only thing which gets created is the new lexical environment that gets enclosed. Using the var self=this;If you closely look at the code, the loop when starts getting executed, it encloses the outer variable along with the lexical environment, which is nothing but a “Closure”. It may be a bit difficult to understand but it will get overtime.
As you know that the arrow(=>) function doesn’t have its own binding of “this”. So, when using the arrow function, there won’t be a new binding to “this”, it will be still using the binding from the loopItNow function which has the local scope of the “unleashThis” object. Using the arrow => function Using the bind method
The value to be passed as the When using Using callback as an argument “this”. If you see the above solution, for-each method takes “this” as a callback and “this” as an argument. “this” encloses the unleashThis from the local object. Few key properties of “this” keyword.
|