JS Foundation first day (global variables, local variables)

Source: Internet
Author: User

Preface: Today I put the variable this piece of content alone, is I think this is also a focus, I know the variable this thing, unfortunately has been half understand, every time a complex writing I have a circle, today I use a good long time to understand this piece of content. Beginners can look at my following cases and detailed explanations , you will suddenly realize that I am today this feeling. Although the daily progress is very slow, but every day has the harvest, this is accumulates many. now I feel the time is not enough, the time is super fast. I don't know what day of the week it is. declaring global Variables1. The variables declared in the outermost execution environment are global variables2. Variables declared without var are global variablesglobal variable Features:The values in the variable can be accessed under any scope.
<script>   Text=5  // global variable   var// global variable     function A () {        a=3// global variable    }</ Script>  

Declaring local variables

1. The variable declared in the function is a local variable2. The formal parameter in the function is a local variable local variable specific: after the function executes, the function life cycle ends. So local variables in the function are deleted by the system, so local variables are not accessible outside of the function unless you use return
<script>   Vara=123//</script>

Global, local variable case studies

function Show () {    var a=b=c=;} Show () Console.log (b,c); Console.log (a);   // The result is a error,   b,c=10 // The students who see this case note the characteristics of the global variables mentioned above

explained in detail: because A is the use of VAR declaration is a local variable, the function outside the access, and the function is not declared a, so error. And b,c does not use VAR declaration, is a global variable, so the function can be accessed outside.

  var Ten ;   function test () {     ;     alert (a);     Alert (this. a);      var A;    Alert (a)   }   // result
In detail, JS performs a full analysis of the declaration portion of the entire file before execution, thus determining the scope of the variable, so before the function test executes. 1, because the 6th line declares the local variable, so a inside the function points to the declared local variables, so the 4th line output 100.2, Line 5th Output THIS.A, we all know that the function inside the this pointer to the caller of the function, where the function test is called by the global object, so this point to the Global Object window, so this.a=window.a, the beginning of the life of the global variable a= 10, so the 5th line result is 10.3, seventh row output result is 100, because the local variable a in line 3rd has been assigned 100, so directly output the value of local variable a.

var  - ; function test () {  console.log (a);   var Ten ;  Console.log (a);} Text ()//Undefinde

Detailed explanation1, in this function, before the execution of line 3rd, you can think that the variable A has been declared, but there is no definition (the assignment here), so the 3rd row result is empty
2, after the execution of line 4th a=10, the value of variable A is 10. So the 5th line outputs the result is ten.

var a =+, function test () {alert (a)  a=//100 10 10 
Detailed explanation1, we all know in the function, the general use of Var declared as a local variable, VAR declaration is generally a global variable 2, in the text function, a=10 declared a global variable, so the 3rd row of a should output the value of the global variable, And before the function execution has declared a global variable and assigned a value of 100, so here the 3rd line of output 1003, fourth row to the global variable a re-assigned value 10, so the value of global variable A is 10, so the fifth row output 10.4, and in the function test outside, 8th row output global variable value, Because the global variable is re-assigned a value of 10, the result is 10.

JS Foundation first day (global variables, local variables)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.