JavaScript variable array objects for detailed usage (1/2)

Source: Internet
Author: User
Tags arrays define local garbage collection variable scope

*
1, Variable type:
JavaScript is a weak type, a variable can store any type, and the run-time type is variable;
-> variables can be converted to type;
*/


/*
2, Variable declaration:
*/
var i;
var index;
var i, index;
var i = 0, index = 2;
/*
Variable declaration, the value is undefined when there is no initial value;
and variables declared by Var cannot be deleted using the delete operator;
Repeated declarations to generate coverage, do not cause errors;
An omitted declaration, the variable is implicitly declared as a global variable; (described in the following section)
*/


/*
3, variable scope:
function partition: A variable declared within a function, which can only run within a function, that is, a local variable; (closures can still be referenced);
Internal variables have higher precedence than global variables; Ex
*/

var g = ' global ';
function Check () {
var g = ' local ';
Console.log (g); Local
}
Check ();

/* variable use VAR declaration as much as possible * *
/* There is 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 complain; The
T is not defined; The
test value is undefined; (both the last declared and assigned variables are undefined)
*/

The

/* variable declaration is suspended in advance/
function f () {
Console.log (test);//undefined
var test = ' Test ';
Console.log (test);//' Test '
}
//Convert to
function f () {
var test;
Console.log (test);//variable declared only, then initialized to undefined
test = ' test ';
Console.log (test);//variable assigned, ' Test '
}
/* Undefined variable and unassigned variable */
Console.log (t);//direct use of variable t;
//Note: When assigning a value directly to a variable, it implicitly treats the variable as global;
var t;//unassigned variable, undefined;

/*
4, basic type and reference type:
number/boolean/null/undefined/basic type;
Array/object/function Reference type
Fifth Edition, 63 pages:
Whether you view a string as an immutable reference type similar to the basic type,
Or consider it as the basic type of internal functionality implementation using reference types, and the results are the same;
That is, the String type behaves as a basic type;
The following example illustrates the difference between the base type and the reference type:
*/

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];

Arrays are reference types, and variables A and B point to the same memory address;
The variable holds the actual value of the base type and holds the reference of the reference type (class pointer);

/*
5, Garbage collection
Reference type is not fixed size, such as: Array, can modify length at any time;
A variable cannot directly save a referenced value, but is stored in a location where the variable holds only a reference to that location.
Therefore, JavaScript dynamically allocates memory to store entities;
Eventually, the memory will be freed for reuse, otherwise it would consume all available memory and cause the system to crash;
JavaScript does not require manual memory release; It uses a method called garbage collection (the method is not visible);
It frees up memory occupied by objects that are no longer in use;
*/
var s = ' Hello ';
var u = s.touppercase ();
s = u; Can no longer get the ' hello ' value;
There is no longer a ' hello ' reference in the environment [no variables pointing to it]
(whether to recycle, is determined by whether there is an assignment)
/*
6, as a variable of the property
Global objects
window, this, Math;
In the browser: Navigator, screen;
Local variables: Calling objects
Calling Object
A global variable is a property of a particular global object, then a local variable is called a property of the calling object (call objects);
The parameters and local variables of the function are stored as properties of the calling object.
(Use a standalone object to store local variables so that JavaScript prevents local variables from overwriting the values of global variables with the same name)
The execution environment for JavaScript
When the JavaScript interpreter executes a function, it creates an execution environment for the function (execution context);
An execution environment is the environment in which all JavaScript code snippets are executed.
A global object is used by an environment that runs JavaScript code that does not attribute any function.
All JavaScript functions run in their own unique execution environment, and have their own invocation objects, which define local variables in the calling object.
The JavaScript interpreter can run scripts in different global execution environments, and these environments are not disjointed and can be referenced to each other;
(Window-iframe);
Deep understanding of variable scopes
Each JavaScript execution environment has a chain of scopes (scope chain) associated with it;
A scope chain is a list of objects or an object chain;
When the JavaScript code needs to query the value of the variable X, it starts looking at the first object on the chain;
If the object has a property named X, then the value of that property is used.
If not, JavaScript continues to query the second object in the chain.
If it is not found, continue to query the next object. And so on ...
Add:
F () Scope-> closure scope-> var variable in scope
-> object's prototype scope-> object class attribute scope
-> Top-level scope (window);
*/

Objects and Arrays


/*
Arrays and objects "JavaScript Authority Guide, Fifth Edition"
*/

/*
Object: is a collection of unordered attributes, each with its own name and value.

/* Create object Simple method, Object direct quantity * *

var obj = {};
var obj = {name: ' Maxthon '};
var obj = {name: {}, Text: []};

/* You can use the new operator/*
var a = new Array ();
var d = new Date ();
var r = new RegExp (' Web effects ', ' I ');
var o = new Object (); var o = {};
/* Note: The new operator is followed by the constructor, so
typeof Array; ' function '
typeof Object; ' function '
Object is an instance of a Function.
A Function is a special object and an instance of object.
*/

/* Object Properties * *
Use. Conforms to the value of the property being accessed.
Note: You can also use [], which uses the attribute name (which is particularly useful for using variables).

var t = {};
T.text = ' Hello ';
T.O = {};
t.o.name = ' rd ';
T.N = [];

var t = {
"Text": "Hello"
};
Console.log (T.text); ' Hello ';

Add: often use keyword var to declare a variable, but when declaring an object property, you cannot use the Var declaration


/* Object Enumeration * *

var F = function () {};
F.prototype.name = ' RD ';
var obj = new F;
for (var key in obj) {
Console.log (key); Name
}

Enumerate only the objects themselves, not along the prototype chain

For
(var key in obj) {
if (Obj.hasownproperty (key)) {
Console.log (key); //
}
}

/* Note: For in cannot enumerate predefined attributes; Tostring. */


/* Check attribute existence * *

window.a = ' rd ';
Console.log (A in window); True

var F = function () {};
F.prototype.name = ' RD ';
var obj = new F;
Console.log (' name ' in obj); True


var toString = Object.prototype.toString;

If Object obj contains method GetName, execute it;

if (Obj.getname && tostring.call (obj.getname) = = ' [object Function] ') {
Obj.getname ();
}

Add:
Console.log (null = = undefined); True
Console.log (null!== undefined); True


/* Delete attributes/*

Delete obj.name;

Add: Using the delete operator, you cannot delete a variable that uses the Var declaration;


/* As an associative array of objects/*

Fetch Object Properties:
Obj.name;
obj[' name ']; Here name is a string.

When using [], the property name is represented by a string. Then you can
Adding operations during run
Note: This is especially useful when this property is passed as a variable.
Also known as associative arrays

/* Mapping: A JavaScript object maps a string (property name) to a value. */
for (var key in obj) {
Console.log (key); The key property name, where the value exists.
}

Home 1 2 last page

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.