1. Both JavaScript and variables are case sensitive.
2. Single quotes and double quotes can be used to create strings without special differences in JavaScript. However, as a general rule, most developers prefer to use single quotes instead of double quotes, but the XHTML specification requires that all attribute values be enclosed in double quotes. In this way, the single quotation marks are used in JS, and the use of double quotation marks on XHTML makes the code of the two more convenient and clearer.
Single quotes can contain double quotes. Likewise, double quotes can also contain single quotes.
3. Brackets
Note that the brackets in Javascript contain two types of semantics, which can be a separator or expression.
A. delimiter everyone is very familiar with (1 + 3) * 3 equals 12
B. (function () {}) (); a pair of parentheses before the function is used as the separator. The parentheses below indicate that this method is executed immediately.
4. function call and reference
Because parentheses represent execution, so:
Var foo = example (); foo indicates the return value of the function.
Var foo1 = example; assign the function reference to a value of foo1
5. Heavy Load
JS does not support overloading. Therefore, here, overloading is more similar to replacement.
JS no difference in the number of parameters
6. Scope and Closure
Scope refers to the code space that has access permissions to a property or method. For example:
Copy codeThe Code is as follows:
Function myFunction (){
Var temp = "abc ";
}
The above temp cannot be accessed outside the function.
A closure is a concept related to the scope. It refers to the attributes of an internal function even after the external function is executed and terminated.
Here is an example of the scope and closure.:
Create the following html page:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Script language = "javascript" type = "text/javascript">
Function init (){
For (var I = 1; I <= 3; I ++ ){
Author = document. getElementById ("author" + I );
Author. onclick = function (){
Alert ("author" + I );
}
}
}
Window. onload = init;
</Script>
</Head>
<Body>
<A id = "author1" href = "#"> abc </a> <br/>
<A id = "author2" href = "#"> abc </a> <br/>
<A id = "author3" href = "#"> abc </a> <br/>
</Body>
</Html>
Running result:
Pic
We can see that this is not what we want.
After modification, the key code is as follows:
Copy codeThe Code is as follows:
<Script language = "javascript" type = "text/javascript">
Function init (){
For (var I = 1; I <= 3; I ++ ){
Author = document. getElementById ("author" + I );
RegisterListener (author, I );
}
}
Function registerListener (author, I ){
Author. onclick = function (){
Alert ("author" + I );
}
}
Window. onload = init;
</Script>
In this way, we get the expected results.
This is because every call to init generates a function instance, and each instance maintains a new
This I already exists at the upper level of the anonymous function, and it is different from the I in init.