JS Getting Started tutorial [garbage collection mechanism]

Source: Internet
Author: User
Tags garbage collection
4, garbage collection mechanism:
Automatically frees up memory in JS.
Like what:
var s = "Heelo";
var B = s.touppercase ();
S=b; After running to here, JS will automatically detect no longer use an object, because S=b, so JS will automatically release the string "Heelo" the storage space occupied. That is, we can no longer get the original "Heelo" value;.
5, JavaScript variables:
JS is non typed. Its variables can be placed in any type of value.
Declaration of variables:
var A;
var b;
Or
var a, B;
Or
var a=0, B=1;
Repeated statements are legal,
If a statement is omitted, JS declares the variable implicitly. Of course, implicitly declared variables are always global variables.
6, the scope of the variable:
JS has 2 kinds: global and Local.
As you can see from the definition of the name, global variables are scoped globally.
In the JS code, there are definitions everywhere.
The scope of a local variable is local.
defined in the function body.
Local variables with the same name have higher precedence than global variables of the same name, as the following example illustrates:
var a = "abc"; Global variables
function Check () {
var a = "EFG"; Local variables with the same name
document.write (a);
}
Check (); Output EFG
Look at one of the more classic examples:
var scope = "global";
function f () {
alert (scope); Output undefined
var scope = "local";
alert (scope); Output Local
}
f ();
Why does the first one output undefined?
Because JS stipulates that the authority variables and global variable names are the same, global variables with the same name in the body of the function are hidden.
So just the example is actually equivalent to:
function f () {
var scope;
alert (scope);
Scope = "local";
alert (scope);
}
f ();
OK, if you read this example, you know a little bit about the difference between local and global.
7, the scope of the variable:
From the inside to the outside:
Lexical scopes
Scope chain
Variable Lookup
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;
global variable x = 1
Is it defined here?
Is
Whether
Get value
Is it defined here?
Is
Whether
Get value
Is it defined here?
Is
Whether
Get value
Not defined
8, client global variables:
In client JS, the Window object represents the browser windows, and he is a global object. 、
For example, our common parseint (), Math () are properties defined by the Window object.
JS allows the execution environment for multiple global variables with different global objects for each environment.
For example: The client JS each independent browser window, or the same window of different frames.
The code is run in its own execution environment and has its own global object.
Of course, you can use an expression parent.frames[0].x; To reference the global variable x in the first frame, so that the code in the different frames is connected.
But there's a security issue here.

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.