JavaScript variables and scopes

Source: Internet
Author: User
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. reference data type storage refers to the address of the object actually stored in the heap memory .... syntaxHighlighter. all (); 1. javaScript variable type
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 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. dynamic attributes
You can add attributes and methods to variables that reference data types, or delete attributes and methods, for example:
[Javascript]
Var p = new Object ();
P. name = "James ";
Alert (p. name); // John
However, this is not allowed for basic data types, as shown below:
[Javascript]
Var str = "James ";
Str. age = 20;
Alert (str. age); // undefined
3. Copy the variable value
For a variable of the basic type, copying from one variable to another is actually copying the value stored in the first variable 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;
[Javascript]
Var num1 = 1;
Var num2 = num1;
Alert (num2); // 1
Num1 = 2;
Alert (num2); // 1
However, for the reference data type, copying a variable to another variable is actually copying the address stored in the first variable 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:
[Javascript]
Var p1 = newObject ();
P1.name = "James ";
P1.age = 20;
Var p2 = p1;
Alert (p2.age); // 20
P1.age = 30;
Alert (p2.age); // 30
4. parameter transfer
JavaScript parameters are passed 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:
[Javascript]
Function inc (num ){
Num + = 1;
}
Var I = 10;
Alert (I); // 10
Inc (I );
Alert (I); // 10
For the basic type variable I, passing it to function inc is actually passing its value 10 to the parameter of function inc. Inside the function, the variable process inside it is added with 1 operation, therefore, I will not be affected.
Let's look at the following code: www.2cto.com
[Javascript]
Function setName (obj ){
Obj. name = "James ";
}
Var person = newObject ();
SetName (person );
Alert (person. name); // John
The person is passed to the setName function, so the heap memory address pointed to by person is passed 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 variable type
Check the basic type by using the typeof operator. For example:
[Javascript]
Var s = "James ";
Var B = true;
Var I = 20;
Var u;
Var n = null;
Var o = new Object ();
Alert (typeof s); // string
Alert (typeof B); // boolean
Alert (typeof I); // number
Alert (typeof u); // undefined
Alert (typeof n); // object
Alert (typeof o); // object
However, in terms of detecting the referenced data type, typeof is not powerful and must use the instanceof OPERATOR:
[Javascript]
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:
[Javascript]
For (var I = 0; I <10; I ++ ){
// Do something
}
Alert (I); // 10
The preceding code is used as an example. The for statement block does not have a special scope. Therefore, I can still be accessed outside of the for loop. Similarly, this is true for the if-else statement.
[Javascript]
If (true ){
Color = "red ";
}
Alert (color); // red
However, there is still a way to temporarily Add a new scope, mainly reflected in try-catch and with statements.
 
 
 
 
Reference books:

JavaScript advanced programming: www.2cto.com/ebook/201007/19189.html

From wawlian:

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.