1. Double Single quotes in js are acceptable.
<A href = "javascript: alert ('hello')"> point </a>
<A href = "#" onclick = "alert ('hello')"> point </a>
2. js Variables
The null variable value is null, and undefine does not point to any object.
Var I = 10;
I = "aaaa ";
I ended with aaaa
I = 10; I is similar to a global variable
3. js debugging
Install DeBugBar to assist debugging
4. Judge variable Initialization
Var x;
If (x = null)
{}
If (typeof (x) = 'undefined ')
{}
If (! X)
{}
The last method is recommended.
If (x) // The variable is initialized north or the variable is not empty or the variable is not 0
5. function declaration
Alert (add (1, 2 ));
Function add (i1, i2) {// it is best not to wrap this braces to avoid bugs.
Return i1 + i2;
}
// Alert (add (1, 2); // alert () can be completed after the function
Anonymous Functions
Var f1 = function (i1, i2) {return i1 + i2 ;}; (note; differences)
Alert (f1 (1, 2 ));
// Declare a function and use it immediately
6. js object-oriented basics:
JavaScript does not have the class syntax. It is simulated using the function closure (closure ).
Function Person (name, age) {// is a function. js does not have a class. Here, a function is used to simulate the class.
This. Name = name; // this. Name dynamic variable
This. Age = age;
This. SayHello = function () {alert (this. Name )};
}
Person p1 = new Person ('dan', 23 );
P1.SayHello ();
[P1.sex = 'male ';
Alert (p1.sex); // also dynamically add attributes]
7. Array (equivalent to ArrayList and Hashtable in c)
Is a dynamic array with no pre-customized size required
Var arr = new Array ();
Arr [0] = "zheng ";
Arr [1] = "dan ";
Arr [2] = "dan ";
For (var I = 0; I <arr. length; I ++ ){
Alert (arr [I]);
}
Example 1:
Function getMax (arr ){
Var max = arr [0];
For (var I = 0; I <arr. length; I ++ ){
If (arr [I]> max ){
Max = arr [I];
Return max;
}
}
}
Var var1 = new Array ();
Var1 [0] = 20;
Var1 [1] = 10;
Var1 [2] = 2;
Var1 [3] = 5;
Int max = getMax (var1 );
Alert (max );
Example 2:
{3, a, 8, haha}
I and length-1 switching position
Function myReverse (arr) {// transfer is not a copy
For (var I = 0; I <arr. length/2; I ++ ){
Var temp = arr [I];
Arr [I] = arr [arr. length-I];
Arr [arr. length-I] = temp;
}
}
Var arr = new Array ();
Arr [0] = "zheng ";
Arr [1] = "dan ";
Arr [2] = "dan ";
Alert (myReverse (arr ));
Example 3:
Split the array string with |. arr. join ("| ")
Function myjoin (arr ){
Var s = arr [0];
If (arr. length <= 0)
Return;
For (var I = 1; I <var. length; I ++ ){
S + = "|" + arr [I];
}
Return s:
}
Var arr = new Array ();
Arr [0] = "zheng ";
Arr [1] = "dan ";
Arr [2] = "dan ";
Alert (myjoin (arr )));
8. Dictionary in js
Array: Array, Dictionary, Stack.
Var diet = new Array ();
Dict ["person"] = "zheng ";
Dict ["Port"] = "dan ";
Dict ["hand"] = "dan ";
Alert (dict ["person"]); // the pop-up result is "zheng ";
[Dictionary with "person" as key and "zheng" as value]
Foreach (var k in dict {
Alert (k); // The retrieved result is the key (I .e., population hand );
}
Note: array usage is a special case of Dictionary (the key is an integer)
9. simplified declaration of Array
Var arr = [1, 2, 3]; // Array
Var nameAge = {"tom": 30, "joy": 20}; // Dictionary
10. Use for to get all the members of an object. The object members appear in the form of object keys.
For (var a in document ){
Alert ();
}
Var p1 = new Object ();
P1.Name = "zheng ";
P1.age = 23;
P1.SayHello = function () {alert ("hello ");};
P1.SayHello ();
Foreach (var a in p1 ){
Alert (e); // get zheng 23 SayHello; e is every key
}
11. js does not need to be compiled and run directly. In the development environment vs, in Notepad
Vs2008SP1VS90SP1-KB958502-x86 these two patches JQuery "." Don't come out and don't affect running
12.
<Script type = "text/javascript"> </script>
Reference external js files. The external files are loaded only once, reducing the load on the website. Real-world sharing.
<Script src = "commonJs. js" type "text/javascript"> </script> [head tag]