Home  >  Article  >  Web Front-end  >  Javascript Introduction to Learning Part 2 js Type_Basic Knowledge

Javascript Introduction to Learning Part 2 js Type_Basic Knowledge

PHP中文网
PHP中文网Original
2016-05-16 19:03:091041browse

The previous article talked about some concepts (lexical structure) and data types (part) in js. Let’s continue in this chapter and then learn about the scope of operating data and functions in js.

1. Conversion between objects and basic types:
Whenever, as long as the object is not empty, it is true in a Boolean environment.
For example;
new Boolean(false);
new Number(0);
new String("");
new Array();
Although the internal value above is false, the value of the object is true;
Object ? valueOf() ? toString()
Among them, the Date class performs toString() conversion first.

2. Manipulating a data value in js:
Any language has its own method of manipulating data;
Js is no exception. There are 3 important ways to manipulate a data value in js.
1) Copy it. For example, assign it to a new variable.
2) Pass it as a parameter to a function or method.
3) Can be compared with other values.

Js operates the values ​​of these data through two methods: passing by value and passing by address.
As you can see from the name, passing by value is to manipulate data by passing values. During the assignment process, the actual value is copied and stored in a new variable. The copied value and the original value are two completely independent values. So if you change the copied value, it won't affect the original value. When comparing sizes, a byte-by-byte comparison is usually performed.
Passing address As the name suggests, it is to manipulate data by passing the address. During the assignment process, the address of the actual value (it can be said to be a reference) is copied. They are not completely independent, so if you change the value through the reference, the original value will also change. When comparing sizes, they are usually compared to see if they reference the same address.
Simple address passing example:
var a = new Date();
alert(a.getDate());
var b = a;
b.setDate(21);
alert(a.getDate()) // Output 21

3, generally speaking:
Basic data types are operated by passing values. (If you forget which basic data types are, you can look back.)
Object data types are operated by passing addresses. (Such as arrays and functions)
Example:
<script> <br>//Pass value<br>a=1; <br>b=a; <br>b=2; <br>alert (a); //Output 1 <br><br>//Address <br>x=[1,2]; <br>y=x; // What is assigned to y is only a reference to x, not x itself. The array has been assigned in the statement. After executing this code, there is still only one array object, but we have two references to it. <br>y[0]=2; <br>alert(x[0] " | " x[1]); //Output 2 | 2 <br></script>
We must pay attention to the characters String:
Strings in js are copied and passed by address, and they are compared by value.
Objects and arrays are passed by value, but the value passed is actually a reference, not the object itself.
Summary: Type Copy and Communication Comparison
Digital Digital Value Value Value
Boolean Dive Value Value Value
Strough Strings Invasion and Uncosty Value The address passed
is immutable: In JS, there is no way to change the content of the string value.
For strings, it doesn’t make much sense whether to pass by value or by address.

4. Garbage collection mechanism:
Automatically release memory in Js.
For example:
var s = “heelo”;
var b = s.toUpperCase();
s=b; //After running here, js will automatically detect that a certain Object, because s=b, js will automatically release the storage space occupied by the string "heelo". That is, we can no longer get the original "heelo" value;.

5, javascript variable:
Js is untyped. Its variables can hold any type of value.

Variable declaration:
var a ;
var b ;
or
var a, b ;
or
var a=0, b=1;

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

6, variable scope:
Js has two types: global and local.
From the definition of the name, we can know that the scope of global variables is global.
In js code, there are definitions everywhere.
The scope of local variables 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 variables
function check(){
var a = "efg"; //Local variable with the same name
document.write(a);
}
check(); // Output efg

Look at a more classic example:
var scope = "global";
function f(){
alert(scope) ; //Output undefined
var scope = "local";
alert(scope); //Output local
}
f();

Why is the first one output? What about undefined?
Because js stipulates that when the names of local variables and global variables are the same, the global variables with the same name in the function body will be hidden.
Then the example just now is actually equivalent to:
function f(){
var scope;
alert(scope);
scope = "local";
alert(scope);
}
f();
OK, if you understand this example, it means that you have a little understanding of some differences between local and global.

7, variable scope:
From inside to outside:
Lexical scope Scope chain Variable lookup
var x = 1;
function f(){
var y =2;
function g(){
var z =3;
}

} Call g() object; z =3;




Call 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 properties defined by the Window object.

Js allows multiple execution environments of global variables, and each environment has different global objects.
For example: each independent browser window of client js, or different frames of the same window.
The code in it all runs in its own execution environment and has its own global object.
Of course, you can use the expression parent.frames[0].x ; to refer to the global variable x in the first frame; this connects the code in different frames.
But there is a security issue here.

Summary;
Mainly talks about "passing by value and passing by address" and the scope of functions.
Slightly difficult to understand for novices. If you still don’t understand, you can search for information on google


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn