Home > front end >  Var a=a=1 why has always been a 1 a 2
Var a=a=1 why has always been a 1 a 2

Time:01-07

A.a ddEventListener (' click ', function () {
Var a=1
A=a + 1
The console. The log (a)
})
The output has been to 2

CodePudding user response:

Each click will reset the value of a, make a statement on the outside of the function is ok

CodePudding user response:

A declared as local variables, here will be a statement on the event, become a global variable

CodePudding user response:

To add static var in front of a=1, a static variable, or for a global variable in function declarations outside can solve the problem of the current

Junction post rate 66.67%
A.a ddEventListener (' click ', function () {
The static var a=1
A=a + 1
The console. The log (a)
})

Or
Var a=1
A.a ddEventListener (' click ', function () {

A=a + 1
The console. The log (a)
})
  • Related