Training Course Title Record 2

Source: Internet
Author: User
Tags new set

This problem seems to be divided into two situations.
Situation one, put the source code first:
The first 12th line is to create the function for F,g. Then when the program reads the self-tuning function of the red-framed part, apart executes the contents directly. Next look at the Red box section:
If the condition has 3 values that are used for relational logic operations, from left to right are: 1, g () The return value 2, the [] boolean value 3,! Boolean value of []
The return value of G () is, obviously, false. The boolean value of [] is true. Why? Logically, [] itself is an array object, it is not a Boolean type of value, how can be used for logical operation? Logical operation, the default is the logical operator (here is &&) the left and right sides of the value first implicitly converted to a Boolean type of value, and then do the logical operation. So G () &&[] is equivalent to: false && Boolean ([]), Boolean ([]) result is true, so there is: false && true== "false. Since [] Take the Boolean value true to get it true, that!  [] Of course it is false to take false. So if the conditions inside the parentheses are actually like this: (False && true) = = False at a glance, = = on both sides compared to the result is true, if condition satisfies, enters if the execution body.

In the execution body, two sentences were executed: var f = function () {return false;} and function g () {return true;}. The former creates a function f in the direct amount of the function, and the latter declares a function g. At this point, there are two new function object generation is no doubt, the question is, the red box within a new set of f and G and the red box is not the same thing? Will there be conflicts? If you judge for more than 2 seconds, then I can only say: "You don't have the power." Obviously not, one group, and the other outside, all the contents of the red box will be discarded after the entire anonymous function call is complete. In other words, the f and G two functions in the red box have just been created and discarded even if the call has not been dropped-the entire red box's tune is meaningless at this moment.
=========================================================================================================
It is too obvious what the console will output when the program finishes executing the ①.

Execution to ② here is equivalent to letting the previously declared F point to a new function, and the result is that the value that F holds is replaced.
(If you're not sure, see what's ahead of the announcement.) It's not much of a problem, but it's still a reminder that the declaration of the function is not the same as the advance rule of the variable, only with: function fun () {...} --Declare the function created by the way, its name can be advanced, with Var fun =function () {...} This function is not a direct measure of the way)

The program executes to ③ and calls the F function, where a is declared within the AO using VAR, so it will be discarded with the AO as the function is called. As for B, or that sentence, never encountered a var declaration on B before, so create a B directly under window and let it be equal to 1. After the execution of the ③, only B was left on the result. So the last one to output an error is 1.

========================================================================================================
Situation two, change the source code to become like this:

The previous content is not said, only from the settimeout part of the beginning, because it involves the implementation of the Environment stack (ECS) This I am not familiar with things, so I left the teacher gave the comments to my opinion to explain, here I hope someone can help me to see whether my point is correct.
settimeout Timer requirements to wait until the execution of the main program is executed before execution, then where is called "The main program is finished"? I think it is until the end of the entire program, that is, ECS will not have new objects into the stack. If each JS execution statement represents an object that enters ECS, it is only when the program reads that nothing can be read to ensure that there is no new content in the execution environment stack. Only at this time does the function in the timer begin to be called. In other words, in this case, the F in the timer is only called until after the settimeout in the diagram and after all the statements after settimeout have been executed. And since F has not been called for a day, there will never be a variable A and B appearing,
So the last Console.log (b) and Console.log (a) are, of course, only error.
To verify the correctness of the above statement, I have modified this part of the source code:

Here is the result of the execution:

Glance.



-------------------------This article by Bo Master original and keep chasing intrestes, if need to reprint please contact Bo Master with permission, and note Reprint source Bo main mailbox: [email protected] Reprint please indicate the source, Welcome experts to discuss together----------------------------

Training Course Title Record 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.