Introduction to Javascript Article 2: Basic knowledge of Javascript type _

Source: Internet
Author: User
The previous article introduced some concepts (lexical structures) and data types (parts) in JavaScript ). In this chapter, we will continue to learn about the data operations and function scopes in js. The previous article introduced some concepts (lexical structures) and data types (parts) in JavaScript ). In this chapter, we will continue to learn about the data operations and function scopes in js.

1. Conversion between objects and basic types:
No matter when the object is not null, it is true in a Boolean environment.
For example;
New Boolean (false );
New Number (0 );
New String ("");
New Array ();
Although the internal value is false, the object value is true;
Object? ValueOf ()? ToString ()
In the Date class, toString () conversion is performed first.

2. operate a data value in js:
Methods for operating data in any language;
Js is no exception. js has three important methods to operate a data value.
1) copy it. For example, assign it to a new variable.
2) pass it as a parameter to a function or method.
3) Compare the value with other values.

Js operates the values of these data in two ways: Pass the value and transfer the address.
You can see from the name that the data is operated by passing the value. During the assignment process, the actual values are copied and stored in a new variable. The copied value and the original value are two completely independent values. Therefore, if you change the copy value, the original value will not be affected. When the data size is compared, the data is usually compared in bytes.
From the perspective of the name, the address is transferred to operate the data. During the value assignment process, the actual value addresses (which can be referenced) are copied. They are not completely independent. Therefore, if you change the value through reference, the original value will also change. When comparing the size, it usually depends on whether they reference the same address for comparison.
Simple address transfer example:
Var a = new Date ();
Alert (a. getDate ());
Var B =;
B. setDate (21 );
Alert (a. getDate () // Output 21

3. In general:
The basic data type is operated by passing values. (If you forget the basic data types, you can go back to them .)
The object data type is operated by transferring the address. (Such as arrays and functions)
Example:
Script
// Pass the value
A = 1;
B =;
B = 2;
Alert (a); // output 1

// Transfer the address
X = [1, 2];
Y = x; // assigned to y is only a reference of x, not x itself. The array has been assigned a value in the statement. After executing this code, there is still only one array object, but we have two references to it.
Y [0] = 2;
Alert (x [0] + "|" + x [1]); // output 2 | 2
Script
Note the following strings:
In js, strings are copied and transferred through address transfer, and they are compared by passing values.
Objects and arrays are passed by passing values, but the passed value is actually a reference, not the object itself.
Summary:
Type replication transfer comparison
Pass numeric values
Boolean value transfer value
Immutable and immutable string value passing
Object address transfer address
Immutable: In JS, there is no way to change the content of the string value.
For a string, it is of little significance to pass the value or the address.

4. Garbage collection mechanism:
The memory is automatically released in Js.
For example:
Var s = "heelo ";
Var B = s. toUpperCase ();
S = B; // after running it here, js will automatically detect that an object is no longer used. Because s = B, js will automatically release the storage space occupied by the string "heelo. That is, we can no longer obtain the original "heelo" value ;.

5. javascript variables:
Js is non-type. Its variables can contain any type of values.

Variable declaration:
Var;
Var B;
Or
Var a, B;
Or
Var a = 0, B = 1;

Repeated declarations are legal,
If the declaration is omitted, js will implicitly declare the variable. Of course, variables declared implicitly are always global variables.

6. Scope of variables:
Js has two types: global and local.
From the definition of the name, we can know that the scope of global variables is global.
JavaScript code is defined everywhere.
The scope of a local variable is local.
Defined in the function body.

Local variables with the same name have a higher priority than global variables with the same name. The following example illustrates this:
Var a = "abc"; // global variable
Function check (){
Var a = "efg"; // local variable with the same name
Document. write ();
}
Check (); // output efg

Let's look at a classic example:
Var scope = "global ";
Function f (){
Alert (scope); // output undefined
Var scope = "local ";
Alert (scope); // output local
}
F ();

Why does the first output undefined?
Because js requires that when the local variables and global variables have the same name, the global variables with the same name in the function will be hidden.
The example is equivalent:
Function f (){
Var scope;
Alert (scope );
Scope = "local ";
Alert (scope );
}
F ();
OK. If you understand this example, you have a little understanding of the differences between local and global.

7. Scope of variables:
From the inside out:
Lexical scope Chain Variable Search
Var x = 1;
Function f (){
Var y = 2;
Function g (){
Var z = 3;
}

} Call the g () object; z = 3;




Call the f () object; y = 2;




Is the global variable x = 1 defined here?
Yes
No
Get value
Is it defined here?
Yes
No
Get value
Is it defined here?
Yes
No
Get value
Undefined

8. Client global variables:
In client js, the Window object represents the browser Window, which is a global object. ,
For example, the commonly used parseInt () and Math () are all properties defined by the Window object.

Js allows the execution environment of multiple global variables. Each environment has different global objects.
For example, each independent browser window of the client js, or different frames of the same window.
The code runs in its own execution environment and has its own global object.
Of course, you can use the expression parent. frames [0]. x; to reference the global variable x in the first frame. In this way, the code in Different frames is linked.
However, security issues exist.

Summary;
It focuses on the scope of value transfer, address transfer, and function.
It is a little difficult for new users to understand. If you still do not understand, you can search for materials on google.

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.