In JavaScript, after a global variable is declared globally, it will become a property of the global object with the same name. However, after a local variable is declared in a function, it will not become an attribute of the function (object). Instead, it must be declared using "function name.attribute name". What is the reason?
It can be understood like this:
Variables declared inside a function belong to the function execution context object, not the function object
Variables declared in the global environment belong to the global execution context object, and this context object is the global environment object
There is no reason, this is the rule.
Historical issues, it is recommended to use strict mode to eliminate confusion.
If you mean this
`function test(){
}`
//Update, error correction.
The questioner has a good look at the basics.
I think this is the scope problem of function variables. js is very flexible. I hope we can learn together^~^ ^~^
In JavaScript, after a global variable is declared globally, it will become a property of the global object with the same name. After a local variable is declared in a function, it becomes a local object, which is an attribute of the function, so you must first access the function globally and then access the local variables in the function.
Function scope