preface
Hello everyone, I am Lin Sanxin, with the most easy to understand the most difficult knowledge points is my motto, the basis is advanced premise is my initial mind.
First of all, this is not the title party, it is true, this question swept dozens of groups, can answer the people is very small, finally even Wang Hongyuan teacher came forward to answer this question.
The title
Why don’t we see what this one looks like
var x = 1;
function f(x, y = function () { x = 3; console.log(x); }) {
console.log(x)
var x = 2
y()
console.log(x)
}
f()
console.log(x)
// //1. What does the above code output?
Var x = 2 var x = 2 var x = 2
// //3. What is the output of f if the first argument x is changed to xx?
// //4. What is the output of f if the first argument x is set to 4 by default?
Copy the code
The first question
In fact, there were a lot of students who got the first question wrong, so you might get two answers
- 1.
Undefined, 3, 3, 1
- 2,
Undefined, 3, 2, 3
In general terms, there are two ideas
- 1, the parameter y in the function
x = 3
Change is theglobal
thex
- 2. The parameter y in the function
x = 3
Change is theInside f
thex
But actually, both of these ideas are wrong, because most of you only see two x’s
- 1. Global
x
- 2. Inside the f function
x
But there’s another x that I’m ignoring
- 3. Parameters of f function
x
This is the key to solving the problem. Let’s have a look at Wang Hongyuan’s explanation
That is, the argument x equals 3 in the y function is actually changing the argument x in the f function, not the global x or the internal x in the F function
So the correct output should be undefined, 3, 2, 1
var x = 1;
function f(x, y = function () { x = 3; console.log(x); }) {
console.log(x) // Parameter x has no default value, so: undefined
var x = 2
y() // Change parameter x and output parameter x, so: 3
console.log(x) // The output is local x, so: 2
}
f()
console.log(x) // Global x has no effect, so: 1
Copy the code
The second question
F console.log(x) is output based on the real time value of x. F console.log(x) is output based on the real time value of x
var x = 1;
function f(x, y = function () { x = 3; console.log(x); }) {
console.log(x) // The parameter has no default value, so: undefined
// var x = 2
y() // change the parameter x = 3 and print the parameter x, so: 3
console.log(x) // The value of real-time parameter x, so: 3
}
f()
console.log(x) // Global x has no effect, so: 1
Copy the code
The third question
Problem 3, if you change the parameter x to the parameter xx, then the parameter y function x = 3 changes the global x, because the parameter x is gone, and because of the proximity rule, the x in the y function refers to the global x
var x = 1;
function f(xx, y = function () { x = 3; console.log(x); }) {
console.log(x) // the variable is promoted but not assigned, so: undefined
var x = 2
y() // x = 3 changes global x and outputs global x, so: 3
console.log(x) // x = 3 changes global x, independent of local x, so: 2
}
f()
console.log(x) // Global x is changed by y function, so: 3
Copy the code
The fourth question
In the fourth problem, the parameter x defaults to 4, and the difference between that and the first problem is whether the parameter x has a default value
var x = 1;
function f(x = 4, y = function () { x = 3; console.log(x); }) {
console.log(x) // Parameter x is the default value, so: 4
var x = 2
y() // change the parameter x = 3, and output the parameter x, so: 3
console.log(x) // The output is local x, so: 2
}
f()
console.log(x) // Global x has no effect, so: 1
Copy the code
conclusion
If you think this article is a little bit helpful to you, click a like and encourage Lin Sanxin haha. Or you can join my shoal of fish to enter the learning group, shoal of fish, please click here to shoal of fish, I will regularly broadcast mock interview, resume guidance, answer questions