Js Variable _ javascript skills

Source: Internet
Author: User
JavaScript is weak type, variables can store any type, and the type is variable during running The Code is as follows:


/*
1. Variable type:
JavaScript is weak, variables can store any type, and the type is variable during running;
-> Variable type conversion is supported;
*/


/*
2. Variable declaration:
*/
Var I;
Var index;
Var I, index;
Var I = 0, index = 2;
/*
Variable declaration. If no initial value exists, the value is undefined;
The variables declared by var cannot be deleted using the delete operator;
Repeated declarations are used to generate overwrite and do not cause errors;
If the declaration is omitted, the variable is implicitly declared and used as a global variable. (next section)
*/


/*
3. variable scope:
Division by function: variables declared inside the function can only be run inside the function, that is, local variables; (closure can still be referenced );
Internal variables take precedence over global variables. ex:
*/
Var g = 'global ';
Function check (){
Var g = 'local ';
Console. log (g); // local
}
Check ();
/* Use var Declaration for variables as much as possible */
/* No block-level scope */
If (false ){
Var test = 2;
Function t (){
Console. log ('t function ');
}
}
T (); // t function;
Console. log (test); // undefined;
/*
Exceptions:
Firefox reports an error;
T is not defined;
The value of test is undefined. (the final declaration and the assigned variables are undefined)
*/

/* Variable declaration will be suspended in advance */
Function f (){
Console. log (test); // undefined
Var test = 'test ';
Console. log (test); // 'test'
}
// Convert
Function f (){
Var test;
Console. log (test); // If the variable is declared only, the initialization is undefined.
Test = 'test ';
Console. log (test); // The variable has been assigned a value, 'test'
}
/* Undefined variables and unassigned variables */
Console. log (t); // directly use the variable t;
// Note: When a value is assigned directly to a variable, it is implicitly treated as a global variable;
Var t; // unassigned variable, undefined;

/*
4. Basic Types and reference types:
Number/boolean/null/undefined/basic type;
Array/Object/Function reference type
Fifth edition, 63 pages:
Whether a string is considered as an immutable reference type with similar behavior and basic type,
Or think of it as the basic type implemented by using the internal functions of the reference type, and the results are the same;
That is, the String type is represented as the basic type;
The following example shows the differences between the basic type and the reference type:
*/
Var a = 3.14;
Var B =;
A = 4;
Console. log (a, B); // 4, 3.14;

Var a = [1, 2, 3];
Var B =;
A [0] = 99;
Console. log (a, B); // same; [99, 2, 3];
// The array is a reference type. Variables a and B point to the same memory address;
// The variable saves the actual value of the basic type and the reference type (class pointer );

/*
5. Garbage Collection
The reference type does not have a fixed size, such as Array. You can modify the length at any time;
A variable cannot directly store the referenced value, but is stored in a certain position. A variable only saves reference to this position.
Therefore, JavaScript dynamically allocates memory to store objects;
The memory will be released for reuse, otherwise all available memory will be consumed, resulting in system crash;
JavaScript does not require manual memory release; it uses a method called garbage collection [method invisible];
It releases the memory occupied by objects that are no longer in use;
*/
Var s = 'hello ';
Var u = s. toUpperCase ();
S = u; // The 'hello' value cannot be obtained;
// There is no 'hello' reference in the environment [no variable points to it]
// (Whether to recycle depends on whether a value is assigned)
/*
6. As a property variable
Global Object
Window, this, Math;
In the browser: navigator, screen;
Local variable: Call object
Call object
Global variables are special attributes of global objects. Therefore, local variables are called call object attributes;
Function parameters and local variables are stored as properties of the called object;
(Using the independent object to store local variables enables JavaScript to prevent local variables from overwriting values of global variables with the same name)
JavaScript Execution Environment
When the JavaScript interpreter executes a function, it will create an execution environment (execution context) for the function );
An execution environment is the environment where all JavaScript code segments are executed.
The environment for running JavaScript code that does not attribute any function uses a global object.
All JavaScript Functions run in their own unique execution environment and have their own calling objects. The local variables are defined in the called objects.
The JavaScript interpreter can run scripts in different global execution environments, and these environments are not out of touch and can be referenced by each other;
(Window-iframe );
Deep understanding of variable scope
Each JavaScript execution environment has a scope chain associated with it );
The scope chain is an object list or object chain;
When JavaScript code needs to query the value of variable X, it starts to view the first object on the chain;
If an object has an attribute named x, the attribute value is used.
If no, JavaScript will continue to query the second object in the chain.
If not found, continue to query the next object. and so on...
Supplement:
F () SCOPE-> closure scope-> var variable scope
-> Object prototype scope-> Object Class Attribute Scope
-> Top-level scope (window );
*/

Related Article

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.