JavaScript No matter where the variable declaration statement appears, it will be executed first before other codes. This article introduces you to the knowledge related to JavaScript variable declaration. Friends who are interested can learn together
JavaScript variable declaration statements will be executed first before other code wherever they appear. Use the var keyword to declare The scope of the variable is the current execution context, which may be a peripheral function, or when the variable is declared outside the function body, it is a global variable.
Those defined outside the function body are global variables, and those defined inside the function body are local variables. The definition here refers to the declaration through var.
JavaScript has the concept of implicit globals, which means that any variable you do not declare will become a global object property. For example:
function test(){ myname = "huming"; alert(myname); } test(); // "huming" alert(myname); //"huming"
The two results are the same, indicating that myname is a global variable.
So, is there any difference between implicit global variables and explicitly defined global variables? . The answer is definitely yes, look at the following example:
// 定义三个全局变量 var global_test = ; global_test = ; // 反面教材 (function () { global_test = ; // 反面教材 }()); // 试图删除 delete global_test; // false delete global_test; // true delete global_test; // true // 测试该删除 alert(typeof global_test); // "number" alert(typeof global_test); // "undefined" alert(typeof global_test); // "undefined"
It can be seen from the above example: global_test1 defined by var outside the function cannot be deleted, and global_test2 and global_test3 that are not defined by var are deleted. Deleted (whether created within the function body or not).
In summary, global variables declared through var outside the function cannot be deleted, but implicit global variables can be deleted.
It should be noted here: JavaScript has a behavior called "hoisting" (suspending/top parsing/pre-parsing).
Let’s use an example to illustrate:
var myname = "huming"; //声明全局变量 function test() { alert(myname); var myname = "local_huming"; alert(myname); } test();
Do you guess the contents of the two alerts are consistent? ? Obviously inconsistent, needless to say consistent. . Actual output is: "undefined", "local_huming".
The above example is equivalent to
var myname = "huming"; //声明全局变量 function test() { var myname; alert(maname);<br> myname = "local_huming"; alert(myname); // "local" } test();
The myname output by the first alert is not the global variable you think, but is in the same scope (a function body) with it. local variables. Although it has not been declared, it is treated as such. This is called "hoisting".
This should make it clear. When you use a variable in a function body and redeclare it later, an error may occur.
Writing specifications:
function test() { var a = , b = , c = a + b, d = {}, e, f; // function body... }
The advantage is:
1. All local variables are defined at the beginning of the function, making it easy to find;
2. Prevent logical errors when variables are used before they are defined.
In JavaScript, a variable name(name) has four ways to enter the scope(scope)
Language built-in, all There are both this and arguments keywords in the scope.
Formal parameters, function parametersare valid in the entire scope
function Declaration
Variable declaration
The four orders listed above are also the order of priority from high to low. Once a variable name has been declared , then it cannot be overridden by other lower priority variable declaration forms.
The above is the detailed content of Detailed explanation of JS variable declaration. For more information, please follow other related articles on the PHP Chinese website!