Home >Web Front-end >JS Tutorial >A brief introduction to js variables_javascript skills

A brief introduction to js variables_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:06:111014browse
Copy code The code is as follows:

/*
1, variable type:
JavaScript is weakly typed, variables can store any type, and the type is variable during runtime;
-> Variables can be type converted;
*/


/*
2, variable declaration:
*/
var i;
var index;
var i, index;
var i = 0, index = 2;
/*
When a variable is declared without an initial value, the value is undefined;
And variables declared with var cannot be deleted using the delete operator;
Duplicate Declaration will generate override and will not cause errors;
omission of declaration will implicitly declare the variable and use it as a global variable; (Introduced in the next section)
*/


/*
3, variable scope:
Divided by function: Variables declared inside the function can only be run inside the function, that is, local variables; (closures can still be referenced);
Internal variable ratio Global variables have high priority; ex:
*/
var g = 'global';
function check() {
var g = 'local';
console.log(g) ; // local
}
check();
/* Use var declarations for variables whenever possible*/
/* No block-level scope*/
if (false) {
var test = 2;
function t() {
console.log('t function');
}
}
t(); // t function;
console.log(test); // undefined;
/*
Exception:
firefox will report an error;
t is not defined;
test value is undefined; (end of declaration and assignment The variables are all undefined)
*/

/* Variable declaration will hang in advance*/
function f() {
console.log(test); // undefined
var test = 'test';
console.log(test); // 'test'
}
// Convert to
function f() {
var test;
console.log(test); // The variable is only declared, so it is initialized to undefined
test = 'test';
console.log(test); // The variable has been assigned a value, 'test'
}
/* Undefined variables and unassigned variables*/
console.log(t); // Use variable t directly;
// Note: When assigning a value to a variable directly, the variable will be implicitly Treated as global;
var t; // Unassigned variable, undefined;

/*
4, basic types and reference types:
Number/boolean/null/undefined/ basic Type;
Array/Object/Function Reference Types
Fifth Edition, page 63:
Whether you think of a string as an immutable reference type that behaves like a primitive type,
or As a basic type implemented using the internal functions of a reference type, the result is the same;
That is: the String type behaves as a basic type;
The following example illustrates the difference between basic types and reference types:
*/
var a = 3.14;
var b = a;
a = 4;
console.log(a, b); // 4, 3.14;

var a = [1 , 2, 3];
var b = a;
a[0] = 99;
console.log(a, b); // Same; [99, 2, 3];
// The array is a reference type, and variables a and b point to the same memory address;
// The variable saves the actual value of the basic type, and saves the reference of the reference type (class pointer);

/*
5, Garbage Collection
Reference types do not have a fixed size, such as: Array, the length can be modified at any time;
Variables cannot directly save the reference value, but are stored in a certain location, and the variable saves is just a reference to this location.
So, JavaScript will dynamically allocate memory to store entities;
Ultimately, this memory must be released for reuse, otherwise it will consume all available memory and cause the system to crash;
JavaScript does not require Release memory manually; it uses a method called garbage collection [method invisible];
It will release the memory occupied by objects that are no longer used;
*/
var s = 'hello ';
var u = s.toUpperCase();
s = u; // The 'hello' value can no longer be obtained;
// There is no longer a 'hello' reference in the environment [no variable points to It]
// (Whether recycling is determined by whether there is an assignment)
/*
6, variables as attributes
global object
window, this, Math;
In the browser: navigator, screen;
Local variables: call object
Call object
Global variables are attributes of a special global object, then local variables are called attributes of the call object ;
The parameters and local variables of the function are stored as properties of the calling object;
(Using independent objects to store local variables allows JavaScript to prevent local variables from overwriting the value of global variables with the same name)
JavaScript execution environment
When the JavaScript interpreter executes a function, it will create an execution context for the function;
An execution context is the environment in which all JavaScript code segments are executed.
Running JavaScript code that does not attribute any function The environment uses global objects.
All JavaScript functions run in their own unique execution environment and have their own calling objects, with local variables defined in the calling objects.
The JavaScript interpreter can be used in different Run scripts in the global execution environment, and these environments are not disconnected and can reference each other;
(window-iframe);
In-depth understanding of variable scope
Each JavaScript execution environment has a JavaScript execution environment associated with it The scope chain;
A scope chain is a list of objects or a chain of objects;
When the JavaScript code needs to query the value of variable X, it starts looking at the first object on this chain;
If the object has an attribute named An object. And so on...
Supplementary:
f() scope-> closure scope-> var variable scope
-> Object's prototype scope-> ; Object class attribute scope
-> Top-level scope (window);
*/


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