SoFunction
Updated on 2025-04-06

Javascript Introduction to Learning Chapter 2 JS Type Page 2/2


4. Garbage collection mechanism:
Automatically release memory in Js.
for example:
var  s  =”heelo”;
var  b   = ();
s=b; //After running here, js will automatically detect that an object is no longer used, because s=b is gone, so js will automatically free up the storage space occupied by the string "heelo". That is, we can no longer obtain the original "heelo" value;.

5, javascript variables:
Js are non-type. Its variable can be placed in any type of value.

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

Repeated statements are legal,
If the declaration is missed, js will implicitly declare the variable. Of course, variables declared implicitly are always global variables.

6. The scope of variables:
There are 2 types of Js: 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 within the function body.

Local variables with the same name have higher priority than global variables with the same name. The following example illustrates this:
var a ="abc"; //Global variable
function check(){
var a = "efg"; // Local variables with the same name
(a); 
}
check(); //  Output efg

Let’s see a classic example:
   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 when the names of local variables and global variables are the same, the global variable 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, The scope of variables:
From the inside to the outside:
Lexical scope   Scope chain    Variable search
var x = 1;
function f(){
  var y =2 ;
 function g(){
   var z =3 ;
}

}    Calling the g() object; z =3;

  


Call the f() object; y =2 ;




Global variable x=1 is it 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 parseInt() and Math() we commonly use are properties defined by Window objects.

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

Summarize;
It mainly talks about the scope of passing values, address and function.
It's a little harder to understand for beginners. If you still don't understand, you can search for information on Google
Or contact me, you can go to my blog and leave me a message:
/?plugins=GuestBookForPJBlog

That’s all today, haha, continue tomorrow. I believe that everyone's js technology will improve step by step.

Previous page12Read the full text