ArticleDirectory
- How to introduce Js in HTML?
- Do not use = to judge floating point numbers.
- Boolean Type
- Function
- Where is the function declared in Javascript loaded?
- Eval () method of the Global Object
- Custom object
- Traverse all elements (attributes and methods) of an object)
- With structure usage
How to introduce Js in HTML?
Javascript can be embedded in all redirected HTML tags.CodeFor example:
<A href = "javascript: Alert ('OK')"> </a>
<Form action = "javascript: Alert ('OK')"> </form>
<SCRIPT> alert ("hello") </SCRIPT>
<SCRIPT src = "test. js"> </SCRIPT> You can import multiple JS files.
Do not use = to judge floating point numbers.
VaR num = 0;
For (VAR I = 0; I <10; I ++ ){
Num + = 0.1;
}
Alert (Num); // num: 0.9999999999999
Boolean Type
Different from. net, only true or false are judged. In JS, 0, 0.00, "", null returns false.
Function
General statement:
Function Test (){};
Function Test (arg1, arg2.. argn ){}
Anonymous functions:
VaR test = function (){
}
Callback Function: (the function can be transferred only when the variable is passed)
Function Test (a, B, FUNA ){
Return FUNA (a, B); // The method execution is determined by the function passed in by the user. The most typical example is that the sort () method of the array can receive specified sorting rules.
}
Function call method:
Function Test (){
Alert ("hello ");
}
Test ();
VaR fun1 = test;
Fun1 (); // functions can also be assigned to variables, so functions are also a data type.
Alert (fun1); // result: outputs the declaration of the entire function (the declaration here includes the function name and code segment)
Function parameters:
Function numadd (num1, num2 ){
Return num1 + num2;
}
Numadd (3, 5) // regular call
Numadd (2, 4, 6, 8, 10) // What about unconventional calls? In fact, fun can also be rewritten as follows:
Function numadd (){
VaR result = 0;
For (VAR I = 0; I <arguments. length; I ++ ){
Result + = arguments [I];
}
Return result;
}
Alert (numadd (2, 4, 6, 8, 10); // result: 30
Default Value of the Parameter
Function Test (a, B ){
If (typeof (A) = "undefined ")
A = 1;
If (typeof (B) = "undefined ")
B = 2;
}
Function Test (a, B ){
If (! A)
A = 1;
If (! B)
B = 2;
}
Function Test (a, B) {// The most concise method
A =? A: 1;
B = B? B: 2;
}
Where is the function declared in Javascript loaded?
The answer is: Window object. In JS, functions are also called Based on objects. Since window objects can be omitted, we can directly call functions we have written!
Eval () method of the Global Object
The eval () method of the global object is equivalent to JSProgramThe Code parser in can execute valid strings, for example:
VaR STR = "Var A =" + F + "; A + ="
VaR F = 5;
Eval (STR );
Alert (a); // 10
Note: If the eval method is not executed, the variable A cannot be obtained.
Custom object
Because there is no class concept, and the object must be new, you must use a function to create an object.
Method 1: This method is the prototype and concept of the initial design.
Function person (){
}
VaR P = new person ();
P. Name = "XJ"; // Property
P. Age = 30;
P. Play = function () {// Method
Document. Write ("playing ......");
}
Method 2:
VaR person = new object ();
Person. Name = "XJ"; // Property
Person. Age = 30;
Person. Play = function () {// Method
Document. Write ("playing ......");
}
Method 3:
Function person (){
VaR P = new object ();
P. Name = "XJ"; // Property
P. Age = 30;
P. Play = function () {// Method
Document. Write ("playing ......");
}
Return P;
}
Get object: var P = person ();
Method 4:
Function person (name, age, play ){
This. Name = Name;
This. Age = age;
This. Play = play; // Method
}
VaR P = new person ("XJ", "30", function {
Document. Write ("playing ......");
});
Alert (P. Name );
P. Play ();
Traverse all elements (attributes and methods) of an object)
For (VAR tmpname in P ){
Alert (P [tmpname]);
}
Note: here we get the element name, not the element value. For... in, we get the index for the array. This is different from the foreach in C.
With structure usage
With (document ){
Write ("hello ");
}
The with statement block makes it easier to use the syntax for changing the attributes and methods of an object. It is suitable for multiple operations on a member of an object.