Functions in JavaScript, JavaScript Functions
Preface
In many cases, the program will repeatedly complete the same or similar tasks during execution. To avoid repeated compilation of the same code segment,
JavaScript wraps part of the code into a module that can be reused. It is called a "function ". Functions can be used in other parts of the program, just as they are
The components of the JavaScript language are the same.
Using functions makes the code easier to read and maintain. A function is one of the basic modules of JavaScript and almost appears in every script.
. 1 basic syntax
Creating a function is like creating a new JavaScript command, which can be used in other parts of the script. Keywords can be used to create a function.
Funcition, followed by the name of the function, followed by a pair of parentheses, followed by a pair of curly braces. The curly braces are the JavaScript language that forms the function.
Example:
<!DOCTYPE html>
. 2 call a function
During page loading, the Code contained in the function definition area will not be executed, but will be executed when called. To call a function, you only need to use the function name.
You can execute the code of the function as needed, as shown in the following example:
<!DOCTYPE html>
The result is as follows:
Place JavaScript code in the
So far, all of our examples have placed JavaScript code in the <body> area of the HTML page. In order to better play the role of the function, I
We need to take a more appropriate approach, that is, place JavaScript code in the
When the src attribute of the <script> element in the
. Putting functions in the head of the document ensures that they have been defined before they are called. For example:
<!DOCTYPE html>
. 3 parameters
If a function can only perform the same operation for each call as in the previous example, its application will be greatly limited. Good news
We can extend the functions of a function by passing data to the function. The implementation method passes one or more "Parameters" to the function when calling the function. See the following example
Example:
<!DOCTYPE html>
We pass two parameters and use alert to display the result of adding two numbers.
The effect is as follows:
. 4 return values from the function
The preceding example shows how to pass parameters to a function to process the data. How can we obtain data from the function? After all, I
You cannot obtain function results only in the pop-up dialog box. To return data from a function, we can use the keyword return. For example:
<!DOCTYPE html>
The above uses the onClick event to call the function sum and input two parameters. The function sum returns the sum of the two values through return and uses the document Object
Write method to output data to the page. (The var keyword is used to declare variables)
The effect is as follows:
Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------
For more information, see http://blog.csdn.net/hai_qing_xu_kong/article/details/48207901render control.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.