current position:Home>Is there anyone to answer

Is there anyone to answer

2022-02-02 21:12:36 CSDN Q & A

0 Basic self-study for a week
Two pictures above and below , Why does the inner loop of the first graph define a new variable ?



Both up and down can be executed normally , What's the difference ?

Refer to the answer 1:

The results should be different , Defined according to requirements , It's not that you will report an error if you modify something , The console passes console.log Print the results and you can see

Refer to the answer 2:

Refer to the answer 3:

No different , It's just that the variable names are the same and different , Just this code will not affect .

If you use a variable with the same name, you should pay attention to that if you use it in other places, it may lead to bug, Because it may be modified by other code, it may cause an error because it is different from the expected value .

Refer to the answer 4:

Refer to the answer 5:

answer : The two results are the same, because the loop in the layer is written in the binding click event , When executing the first layer of loop , The second level loop does not execute , Only after clicking will the second level loop be executed , The two cycles have nothing to do with , It just uses the same global variable , And they are initialized to 0 了

problem : If you do , The variable used for the bound click event i , You have to be able to get , Because I see you for(i=0... It's not here for(let i =0
So you can either i Defined as a global variable , Because you didn't report it wrong , So I guess that's what you did

or for It's defined in the loop for(let i=0;i<item.length;i++)
The same is true with two variables
If help , Excuse me 【 Adopt this answer 】

Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended