Home > front end >  Why two of the same function, return the result of the different?
Why two of the same function, return the result of the different?

Time:01-07

The makeCounter the function, why call it directly and return are 0,
And assign it to a variable y, but returns 0, have played an important role in the count?

Assignment of this an action, can change the function return value?

 function the makeCounter () {
Let count=0;
Return the function () {
Return count++;
};
}

Alert (the makeCounter () ());//0
Alert (the makeCounter () ());//0
Alert (the makeCounter () ());//0

Assigned to the variable y:
Let y=the makeCounter ();
Alert (y ());//0
Alert (y ());//1
Alert (y ());//2


CodePudding user response:

 
This is serious about closure principle, every call the makeCounter () have a closure environment,
In each closure environment to create a separate count variable
Alert (the makeCounter () ());//0
Alert (the makeCounter () ());//0
Alert (the makeCounter () ());//0
This is to call 3 times the makeCounter (), creates three count variable
The makeCounter () () call returns after the function () {return count++; } function,
Function in the operation of the count count is function closures environment variables,
Each function in the operation of the count count is different closure environment variables,
Is similar to the following
If (true) {
Let count=0;
Alert (count++);
}
If (true) {
Let count=0;
Alert (count++);
}
If (true) {
Let count=0;
Alert (count++);
}


Assigned to the variable y:
Let y=the makeCounter ();
Alert (y ());//0
Alert (y ());//1
Alert (y ());//2
Is only 1 call the makeCounter (), just create a count variable
The return of the function () {return count++; } function assigned to y,
Y function called 3 times in the operation of the count variable are the same,
Is similar to the following
If (true) {
Let count=0;
Alert (count++);
Alert (count++);
Alert (count++);
}

CodePudding user response:

refer to the second floor sky waves reply:
 
This is serious about closure principle, every call the makeCounter () have a closure environment,
In each closure environment to create a separate count variable
Alert (the makeCounter () ());//0
Alert (the makeCounter () ());//0
Alert (the makeCounter () ());//0
This is to call 3 times the makeCounter (), creates three count variable
The makeCounter () () call returns after the function () {return count++; } function,
Function in the operation of the count count is function closures environment variables,
Each function in the operation of the count count is different closure environment variables,
Is similar to the following
If (true) {
Let count=0;
Alert (count++);
}
If (true) {
Let count=0;
Alert (count++);
}
If (true) {
Let count=0;
Alert (count++);
}


Assigned to the variable y:
Let y=the makeCounter ();
Alert (y ());//0
Alert (y ());//1
Alert (y ());//2
Is only 1 call the makeCounter (), just create a count variable
The return of the function () {return count++; } function assigned to y,
Y function called 3 times in the operation of the count variable are the same,
Is similar to the following
If (true) {
Let count=0;
Alert (count++);
Alert (count++);
Alert (count++);
}



Also in the understanding of your content, first thank you for your detailed answer,
  • Related