The basic composition of the JavaScript scripting language is programmed by control statements, functions, objects, methods, attributes, etc. to implement.
First, the program control flow
In either language, the program control flow is necessary, which makes the entire program reduce confusion, making it performed smoothly in a certain manner. Below is the program control flow structure and statement commonly used by JavaScript:
1, IF condition statement
Basic format
IF (expression)
Sentence section 1;
......
Else
Sentence section 2;
.....
Function: If the expression is TRUE, the segment segment 1 is executed; otherwise the segment segment 2 is executed.
Description:
The if -else statement is the most basic control statement in JavaScript, which can change the execution order of the statement.
The relationship statement must be used in the expression to implement judgment, which is estimated as a boolean value.
It transforms zero and non-zero numbers to false and true.
If the statement after IF has multiple lines, you must enclose it using the curneth.
IF statement nested
IF (Boolean) statement 1;
Else (Boolean) statement 2;
Else if (Boolean) statement 3;
......
ELSE statement 4;
In this case, the Boolean expression of each stage will be calculated. If it is true, the corresponding statement is executed, otherwise the ELSE is executed.
2, for loop statement
Basic format
For (initialization; condition; increment)
Statement set;
Function: Implement the conditional cycle, when the condition is established, the statement set is executed, otherwise the cyclic body is jumped out.
Description:
The initialization parameter tells the beginning of the cycle, and the initial value of the variable must be given;
Condition: It is a condition for discriminating the cycle stop. If the condition is met, the cyclic body is executed, otherwise it is jumped out.
Incremental: Main Defining Cyclic Control Variables Changes in what ways during each cycle.
Between three main statements, comma separation must be separated.
3, WHILE cycle
Basic format
WHILE (Condition)
Statement set;
The statement is the same as the For statement, and when the condition is true, repeat the loop, otherwise it will exit the loop.
For with while statement
Both statements are cyclic statements, using the for statement, more easy to understand when processing numbers, and is more compact; while the While cycle is more special.
4, BREAK and Continue statements
As in the C language, use the BREAK statement to make the loop from the For or While out, Continue enters the next loop in the loop from the FOR or WHILE.
Second, functions
The function provides programming staff with a full-constant ability. When a complex program is designed, the program is always divided into some relatively independent portions based on the functionality to be completed, and one function is written. Thus, each part is fully independent, the task is single, clear, easy to understand, easy to read, and easy to maintain. The JavaScript function can encapsulate modules that may be used multiple times in the program. And can be called as an event-driven result. Thus, a function is implemented to associate it with the event driver. This is a place with other languages.
1, JavaScript function definition
Function function name (parameter, zero) {
Functional body;
Return expression;
}
Description:
When the modulation function is called, the variables used can be transmitted as a variable. The function is defined by the keyword function.
Function name: Define the name of its own function.
The parameter table is the value passed to the function or action, which can be constant, variables, or other expressions.
Call a function by specifying a function name (inform).
You must return the value using Return.
The function name is sensitive to case.
2, form parameters in the function:
In the definition of a function, we have a parameter table after the function name, which may be one or more variables. So how can I determine the number of parameter variables? In JavaScript, you can check the number of parameters via arguments .length. example:
Function function_name (exp1, exp2, emp3, eXP4)
Number = function _name. Arguments .length;
IF (Number> 1)
Document.wrile (EXP2);
IF (Number> 2)
Document.write (EXP3);
IF (Number> 3)
Document.write (EXP4);
...
Third, event driver and event processing
1. Basic concept
JavaScript is a language based on object-based. This is different from Java, Java is an object-oriented language. Based on object-based characteristics, event-driven is employed. It is in an environment in which everything is simplified. Usually the action of the mouse or hotkey is called event (Event), and a series of programs caused by the mouse or hotkey, called event driver. For the event, the event is handled or function, we call an event handler (Event Handler).
2, event handler
The processing of object events in JavaScript is usually served by a function (Function). Its basic format is the same as the function, and all functions described above can be handled as an event handler. The format is as follows:
Function event processing name (parameter table) {
Event processing statement set;
......
}
3, event driver
The event in the JavaScript event drive is caused by the movement of the mouse or hotkey. It mainly has the following events:
(1) Click Event Onclick
When the user clicks the mouse button, an OnClick event is generated. At the same time, the event handler or code specified by ONClick will be called executed. Usually produced in the following basic objects:
Button (Button Object)
Checkbox (check box) or (check list box)
Radio (single button)
Reset Buttons (Important Button)
Submit Buttons (Submit button)
Example: You can activate the Change () file by the following buttons: