Take a closer look at <JavaScript authoritative guide>. Take notes.
1. Variable reputation
We all know that javascript can be implicitly named as a variable. However, you must note that the implicit name variable is always created as a global variable. Check the following code to force the declaration of variables in JavaScript. We recommend that you declare variables using VAR. Java code
- <Script language = "JavaScript">
- Function Test ();{
- VaR A = 222;
- Document. writeln ();;
- }
- Test ();;
- Document. writeln ();;
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">function test();{var a=222;document.writeln(a);;}test();;document.writeln(a);;</SCRIPT>Java code
- <Script language = "JavaScript">
- Function Test ();{
- A = 222;
- Document. writeln ();;
- }
- Test ();;
- Document. writeln ();;
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">function test();{a=222;document.writeln(a);;}test();;document.writeln(a);;</SCRIPT>
2. Scope of Variables
Guess what the following code outputs. Java code
- <Script language = "JavaScript">
- VaR x = '000 ';
- Document. writeln (x );;
- A ();;
- Function ();{
- VaR x = 'aaa ';
- Function B ();{
- Document. writeln (x );;
- VaR x = 'bbb ';
- Document. writeln (x );;
- }
- B ();;
- Document. writeln (x );;
- }
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">var x='000';document.writeln(x);;a();;function a();{var x='aaa';function b();{document.writeln(x);;var x='bbb';document.writeln(x);;}b();;document.writeln(x);;}</SCRIPT>
If your answer is 000 undefined bbb aaa. Congratulations, OK. when the Code uses the X variable, it first looks for it from the function block (called object in the authoritative guide). If it cannot be found, it looks for the function block at the upper level until it is found, if the top-level code (indicating the location where var x = '000';) is not defined, the Code reports an undefined error.
Change the code to get the 000 undefined 111 111 Java code.
- <Script language = "JavaScript">
- VaR x = '000 ';
- Document. writeln (x );;
- A ();;
- Function ();{
- Function B ();{
- Document. writeln (x );;
- Document. writeln (x );;
- }
- Document. writeln (x );;
- VaR x = '20140901 ';
- B ();;
- }
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">var x='000';document.writeln(x);;a();;function a();{function b();{document.writeln(x);;document.writeln(x);;}document.writeln(x);;var x='111';b();;}</SCRIPT>
3. New Problems
The scope of variables is clear. Pay attention to the above Code. Why can I call function a before function a () is defined, and my var x = '000000'; the former "cannot be used" X ???
Let me give my understanding one by one
First of all: the following code makes me believe that JavaScript has a pre-compilation process, not fully interpreted and executed in order. Java code
- <Script language = "JavaScript">
- A ();;
- Function ();{
- Alert ();;
- }
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">a();;function a();{alert();;}</SCRIPT>
I personally understand that this pre-compilation process will not compile the code into a language recognized by the virtual machine like Java/C #, nor compile the code into a lower-level language like VB and VC. The conjecture is that this function is only preloaded to the global environment of this function execution. In this execution environment, this function has been identified and defined and can be used directly. (Seeing the Javascript Implementation of AOP written by many people on the Internet, in fact, this pre-compilation process is the best time to translate metadata. Unfortunately, JavaScript is outdated)
This article focuses on some issues with variables. The variable says, why is the function available? I cannot use the variable. Java code
- <Script language = "JavaScript">
- Document. writeln ();;
- VaR a = 0;
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">document.writeln(a);;var a=0;</SCRIPT>
Why do I need to output undefined? Why can't I pre-compile one?
Let's take a look at what the following two pieces of code will output ??? Java code
- <Script language = "JavaScript">
- Document. writeln ();;
- A = 0;
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">document.writeln(a);;a=0;</SCRIPT>
Java code
- <Script language = "JavaScript">
- Document. writeln ();;
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">document.writeln(a);;</SCRIPT>
Maybe you have tried it. Maybe you already know that A is not defined. Haha, okay.
Now I am sure that VaR a = 0; has been pre-compiled by the javascript interpreter, at least recorded. Even set its value to undefined. The name of the word "undefined" is very misleading. How can it be called "undefined"? It is clearly the initial value of all variables in JavaScript. I really don't want to mention the comparison between null and undefined.
Note that the above two sections of code also reflect a phenomenon. Implicitly declared variables are defined as global variables only when they are interpreted.
For different processing of functions and variable JavaScript pre-compilation, you can compare them with the loading process of Java class. Java also sets an output value for the basic type, and the object is null. (Don't go far)
4. Differences between undefined variables and unattached value variablesJava code
- <Script language = "JavaScript">
- VaR;
- Document. writeln ();;
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">var a;document.writeln(a);;</SCRIPT>
Java code
- <Script language = "JavaScript">
- Document. writeln ();;
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">document.writeln(a);;</SCRIPT>
Undefined variables and undefined variables are defined in the Chinese version of the authoritative guide. Through the third analysis, I think the difference between variables should be defined and undefined. The undefined variable does not conflict with the undefined variable. Javascript is not a strongly typed language and the default value is not provided.
5. Basic and reference types
Java users may be familiar with this part. Nothing
First.
6. Javascript Garbage Collection
I have never heard of this part. This issue is mentioned in two sections in the Javascript authoritative guide.
There is no fixed size for strings, objects, and data, and the memory must be allocated dynamically for them. But when will the memory be recycled? Javascript uses the same garbage collection method as Java. Java code
- VaR S = "hello ";
- VaR u = S. touppercase ();;
- S = u;
var s="hello";var u=s.toUpperCase();;s=u;
After running this code, "hello" will be used again without a variable. This is the garbage collection of the "hello" bucket. For Javascript garbage collection, the only thing you need to care about is that it will definitely do it, so don't worry about the memory.
Note that JavaScript does not provide any computation or statement that forces garbage collection or memory release.
The delete operation in Javascript is different from that in C ++. Java code
- <Script language = "JavaScript">
- VaR o = new object ();;
- O. Name = "zkj ";
- O. Age = 25;
- O. Bir = new date ();;
- For (var key in O );{
- Document. writeln (Key + ':' + O [Key] + '</BR> ');;
- }
- Document. writeln ('delete O. Bir </BR> ');;
- Delete O. BIR;
- For (var key in O );{
- Document. writeln (Key + ':' + O [Key] + '</BR> ');;
- }
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">var o=new Object();;o.name="zkj";o.age=25;o.bir=new Date();;for(var key in o);{document.writeln(key+':'+o[key]+'</br>');;}document.writeln('delete o.bir</br>');;delete o.bir;for(var key in o);{document.writeln(key+':'+o[key]+'</br>');;}</SCRIPT>
7. Attributes
Guess what the following code will output. Java code
- <Script language = "JavaScript">
- VaR x = 100;
- Document. writeln (x );;
- Add (x );;
- Document. writeln ('</BR> ---------------------- </BR> ');;
- VaR x = 200;
- Document. writeln (x );;
- Add (x );;
- Function add (x );{
- Document. writeln (x );;
- VaR x = 300;
- Document. writeln (x );;
- VaR x = 400;
- Document. writeln (x );;
- }
- </SCRIPT>
<SCRIPT LANGUAGE="JavaScript">var x=100;document.writeln(x);;add(x);;document.writeln('</br>------------------------</br>');;var x=200;document.writeln(x);;add(x);;function add(x);{document.writeln(x);;var x=300;document.writeln(x);;var x=400;document.writeln(x);;}</SCRIPT>
It is estimated that many people can get the correct answer
100 100 300 400
------------------------
200 200 300 400
But here I want to introduce the concept of global objects and calling objects (the Javascript authoritative guide is so translated) Java code
- <Script language = "JavaScript">
- VaR x = 100; // we have added a Global Object attribute X. Comparison
- // Var o = new object (); O. X = 100;
- Document. writeln (this. X); // use this to access the global object.
- Add (this. X); // pass the attribute value of the global object to the function.
- Document. writeln ('</BR> ---------------------- </BR> ');;
- This. x = 200; // modify the X attribute in the global variable.
- Document. writeln (window. X );;
- Add (window. X );;
- Function add (x );{
- // Assume that there is a local object, the called object, and the object in the function call process.
- // Temp. x =$ {input value}
- Document. writeln (x); // you can print the value in the parameter, that is, temp. x = This. X.
- // Value,
- VaR x = 300; // overwrite the signature of the called object variable.
- Document. writeln (x); // print the modified value. Temp. x
- VaR x = 400; // temp. x = 400
- Document. writeln (x );;
- }
- </SCRIPT>
<Script language = "JavaScript"> var x = 100; // a property X is added to the global object. comparison // var o = new object (); O. X = 100; document. writeln (this. x); // use this to access the global object add (this. x); // pass the attribute value of the global object to the document. writeln ('</BR> ---------------------- </BR>'); this. X = 200; // modify the X attribute of the global variable to document. writeln (window. x); add (window. x); function add (x); {// assume that there is a local object, the called object, and the object // temp during the function call. X =$ {input value} document. writeln (x); // The printed value is the value in the parameter, that is, temp. X = This . X // value, VAR x = 300; // overwrite .doc ument. writeln (x); for the variable signature of the called object; // print the modified value. Temp. xvar x = 400; // temp. x = 400document. writeln (x); ;}</SCRIPT>
In the process of calling a function, assume that a calling object exists. The function parameters and temporary variables in the function are treated as the properties of the calling object. Of course, the life cycle of this called object is very short.
Note: when accessing the attributes of global variables into X, you do not need to use this. X or window. X to access them. Confusion occurs when there is a page with <frame> <IFRAME>.
I will discuss functions in detail later.