The promotion of JS variable name and function name __ function
Source: Internet
Author: User
Important) The function declaration is that the Declaration and implementation of the function are promoted. function expressions and variable expressions only their declarations have been elevated.
Today, a friend of mine asked a question:
The result is printed: A is not a function. When I saw this, I was the first to think of the same variable name and function name, and there was a conflict. So I set the variable name and function name to be different.
So there is actually a problem, the function name and variable name of the promotion problem.
This is the question that my colleague told me about the variable name promotion.
The analysis is as follows: One
variableScope is the region of the variable that is defined in the program's source code.
Global Variableshas a global scope that can be defined anywhere in JS.
Local Variableshas a local scope, defined within the function.
In the function body, the precedence of the local variable
higher thanGlobal variables. If you declare a variable with the same name as a local variable in the function body, the original local variable is overwritten.
From the first module, we can see that the local variables are scoped only within the function, and the local variables are destroyed when the function body is out. In the nested () function, although a scope is declared, the scope in nested () is a local variable, just like the name of a global variable, not a global variable, so although the scope is assigned to "local variables within a nested scope" in the function, But this is just a variable with the same name as a global variable, and does not change the value of the global variable.
The second module see: See here is not some Meng force, this is not the same as just now, why the second frame is not the same. In the above section of the code, in the nested () function, we do not declare scope with VAR, so the scope of this area is elevated, that is, we reset the value of the scope in the Checkscope, So the output at the time of the output is a local variable within the nesting scope.
function expressions and variable expressions are only the declarations that are promoted, and function declarations are the declarations and implementations of functions that are promoted.
One, the variable ascension
Before ES6, JavaScript has no block-level scope (a pair of curly braces {} is a block-level scope), only global scope and function scope.
A variable elevation promotes a variable declaration to the beginning of its scope. Here's a simple example
Because of the variable elevation of JS, the above code is actually executed in accordance with 2.
Second, function promotion JS Create a function in two ways: function declaration and function literal. Function elevation exists only for function declarations.
The following is an example of an analysis:
can be resolved into:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.