Javascript variables and scopes JavaScript variables and scopes

Source: Internet
Author: User
Tags variable scope

Javascript variables and scopes

1. javascript variable types JavaScript variables are classified into basic types and reference types. the basic data type is a simple data segment that directly has a stack space. This type directly stores the value at a location in the memory. the referenced data type stores the addresses of objects actually stored in the heap memory. there are five basic data types in javascript: Number, null, undefined, Boolean, and string. note that the string in JS is the basic data type. access to basic type variables is accessed by value, while access to referenced variables is accessed by reference. 2. for variables that reference data types, you can add attributes and methods to them, or delete attributes and methods, for example:
1 var P = new object (); 2 p. Name = "Zhang San"; 3 alert (P. Name); // Zhang San
Copying code is not allowed for the basic data type, as shown in the following code:
1 var STR = "Zhang San"; 2 Str. Age = 20; 3 alert (Str. Age); // undefined
Copy code 3. Copy the variable value for a variable of the basic type from one variable to another. In fact, the value stored in the first variable is copied to another variable. Therefore, after the replication is complete, modify the first variable. The modification operation does not affect the value of another variable. For example;
1 var num1 = 1;  2 var num2 = num1;  3 alert(num2);//1 4 num1 = 2;  5 alert(num2);//1
Copy the Code but copy a variable to another variable for the reference data type. In fact, the address stored in the first variable is copied to another variable. In other words, the two variables then point to the same memory space. Therefore, the next operation on the first variable will also affect the second variable. For example:
1 var p1 = newobject (); 2 p1.name = "Zhang San"; 3 p1.age = 20; 4 var P2 = p1; 5 alert (p2.age); // 20 6 p1.age = 30; 7 alert (p2.age); // 30
Copy Code 4. Pass JavaScript parameters by value. That is, copy the external value of the function to the parameter of the function. For basic types, the replication is the same as the replication of variables. For referenced data types, the replication of referenced data types is the same. The following are examples:
1 function inc(num) {  2     num += 1;  3 }  4 var i = 10;  5 alert(i);//10 6 inc(i);  7 alert(i);//10 
Copy the code to pass the basic type variable I to function Inc. In fact, the value 10 is passed to the parameter of function Inc. Within the function, add one operation to the variable process inside it, so it does not affect I. Let's look at the following code:
1 function setname (OBJ) {2 obj. name = "James"; 3} 4 var person = newobject (); 5 setname (person); 6 alert (person. name); // Zhang San
Copy the code to pass the person to the setname function, and then pass the heap memory address pointed to by the person to the function parameter. Therefore, the memory pointed to by the parameter and the memory pointed to by the person are the same address. Therefore, modifying the content that points to the object inside the function will also be reflected in the person. 5. Check the basic types of variables. Use the typeof operator. For example:
1 var S = "zhangsan"; 2 var B = true; 3 var I = 20; 4 var U; 5 var n = NULL; 6 var o = new object (); 7 alert (typeof S); // string 8 alert (typeof B); // Boolean 9 alert (typeof I); // Number 10 alert (typeof U ); // undefined 11 alert (typeof N); // object 12 alert (typeof O); // object
Copy the Code. However, in terms of detecting the referenced data type, typeof is not powerful. You need to use the instanceof OPERATOR:
1 alert (person instanceof object); // is the person object type?

6. variable scope

JS has two main scopes: global scope and local scope. It is worth mentioning that in JS, there is no block-level effect. For example:
1 for(var i = 0; i < 10; i++) {  2 //do sth 3 }  4 alert(i);//10 
Copy the code above as an example. The for statement block does not have a special scope, so I can still be accessed outside of the for loop. Similarly, this is true for the IF-else statement.
1 if(true) {  2     color = "red";  3 }  4 alert(color);//red
Copy code

However, there is still a way to temporarily Add a new scope, mainly reflected in try-catch and with statements.

Reference Book: javascript advanced programming Original: primitive JavaScript variables are classified into basic types and reference types. the basic data type is a simple data segment that directly has a stack space. This type directly stores the value at a location in the memory. the referenced data type stores the addresses of objects actually stored in the heap memory. there are five basic data types in javascript: Number, null, undefined, Boolean, and string. note that the string in JS is the basic data type. access to basic type variables is accessed by value, while access to referenced variables is accessed by reference. 2. for variables that reference data types, you can add attributes and methods to them, or delete attributes and methods, for example:
1 var P = new object (); 2 p. Name = "Zhang San"; 3 alert (P. Name); // Zhang San
Copying code is not allowed for the basic data type, as shown in the following code:
1 var STR = "Zhang San"; 2 Str. Age = 20; 3 alert (Str. Age); // undefined
Copy code 3. Copy the variable value for a variable of the basic type from one variable to another. In fact, the value stored in the first variable is copied to another variable. Therefore, after the replication is complete, modify the first variable. The modification operation does not affect the value of another variable. For example;
1 var num1 = 1;  2 var num2 = num1;  3 alert(num2);//1 4 num1 = 2;  5 alert(num2);//1
Copy the Code but copy a variable to another variable for the reference data type. In fact, the address stored in the first variable is copied to another variable. In other words, the two variables then point to the same memory space. Therefore, the next operation on the first variable will also affect the second variable. For example:
1 var p1 = newobject (); 2 p1.name = "Zhang San"; 3 p1.age = 20; 4 var P2 = p1; 5 alert (p2.age); // 20 6 p1.age = 30; 7 alert (p2.age); // 30
Copy Code 4. Pass JavaScript parameters by value. That is, copy the external value of the function to the parameter of the function. For basic types, the replication is the same as the replication of variables. For referenced data types, the replication of referenced data types is the same. The following are examples:
1 function inc(num) {  2     num += 1;  3 }  4 var i = 10;  5 alert(i);//10 6 inc(i);  7 alert(i);//10 
Copy the code to pass the basic type variable I to function Inc. In fact, the value 10 is passed to the parameter of function Inc. Within the function, add one operation to the variable process inside it, so it does not affect I. Let's look at the following code:
1 function setname (OBJ) {2 obj. name = "James"; 3} 4 var person = newobject (); 5 setname (person); 6 alert (person. name); // Zhang San
Copy the code to pass the person to the setname function, and then pass the heap memory address pointed to by the person to the function parameter. Therefore, the memory pointed to by the parameter and the memory pointed to by the person are the same address. Therefore, modifying the content that points to the object inside the function will also be reflected in the person. 5. Check the basic types of variables. Use the typeof operator. For example:
1 var S = "zhangsan"; 2 var B = true; 3 var I = 20; 4 var U; 5 var n = NULL; 6 var o = new object (); 7 alert (typeof S); // string 8 alert (typeof B); // Boolean 9 alert (typeof I); // Number 10 alert (typeof U ); // undefined 11 alert (typeof N); // object 12 alert (typeof O); // object
Copy the Code. However, in terms of detecting the referenced data type, typeof is not powerful. You need to use the instanceof OPERATOR:
1 alert (person instanceof object); // is the person object type?

6. variable scope

JS has two main scopes: global scope and local scope. It is worth mentioning that in JS, there is no block-level effect. For example:
1 for(var i = 0; i < 10; i++) {  2 //do sth 3 }  4 alert(i);//10 
Copy the code above as an example. The for statement block does not have a special scope, so I can still be accessed outside of the for loop. Similarly, this is true for the IF-else statement.
1 if(true) {  2     color = "red";  3 }  4 alert(color);//red
Copy code

However, there is still a way to temporarily Add a new scope, mainly reflected in try-catch and with statements.

Reference Book: javascript advanced programming Original: http://www.cnblogs.com/wawlian/archive/2012/02/18/2357271.html

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.