Learning notes for defining and calling JavaScript Functions

Source: Internet
Author: User

You can use the function to define the function in js. Call the function name directly. If there is a parameter, you can directly use the parameter, next I will introduce you to the definition and call examples of JavaScript Functions.

JavaScript function definition

The function definition uses the keyword function. The syntax is as follows:
Function funcName ([parameters]) {
Statements;
[Return expression;]
}

Meanings of each part of the function:

• FuncName is the function name. The function name can be defined by the developer. It is the same as the variable naming rule.
• Parameters is the function parameter. When calling a function, you must pass the actual data to the parameter list to complete the function. One or more parameters can be defined in the parameter list. Multiple parameters are separated by commas (,). Of course, the parameter list can be empty. The parameters here are "form parameters.
• Statements is the function body. The function Body specifies the function, which is the main part of the function.
• Return specifies the return value of the function. A function can use the return statement to specify the return value, or it can return no value. When a function is executed to a return statement, the execution of the function is completed regardless of the Code.
JavaScript function call
After the function is defined, you can call it. When calling a function, you only need to add parentheses after the function name.

If the defined function needs to pass parameters, you need to add parameters in parentheses. Multiple parameters are separated by commas. The parameters here are "real parameters.


The following defines a function that uses document. write () to output information.

The Code is as follows: Copy code

Function print (msg ){
Document. write (msg );
}

Var welcome = "welcome to NowaMagic! Www. bKjia. c0m ";
Print (welcome );
Function print (msg ){
Document. write (msg );
}
 
Var welcome = "welcome to NowaMagic! ";
Print (welcome );

Run the demo: welcome to NowaMagic! Www. bKjia. c0m

JavaScript is a loose type language, so you cannot specify a data type for function parameters, and JavaScript does not check whether the transmitted data is the type required by that function.

Let's look at a JavaScript function example. This function calculates the square of a certain number.

The Code is as follows: Copy code

 

Function square (x ){
Return x * x;
}

Function print (msg ){
Document. write (msg );
}

Var I = 5;
Var result = square (I );
Print (result );
Function square (x ){
Return x * x;
}
 
Function print (msg ){
Document. write (msg );
}
 
Var I = 5;
Var result = square (I );
Print (result );

Run Demo: 25

In some style programming languages, or in some well-defined programming, it is very useful to always use short function names. For example, Prototype in JavaScript elegantly uses a function name $ () (yes, only one dollar sign) to replace document. getElementById (), which is common but difficult to input (). Dollar signs $ and underscore _ are symbols that can be used for JavaScript symbols in addition to numbers and letters.

See the following code:

The Code is as follows: Copy code


<SCRIPT language = javascript>
Function $ (dom)
{
Return document. getElementById (dom );
}
Var con = $ ('listcontainer ');
</SCRIPT>

JavaScript allows the use of functions to define functions directly. A function is an expression that defines an anonymous function. The syntax of a function's direct quantity is very similar to that of a function statement, except that it is used as an expression rather than a statement and does not need to specify a function name.

The following program defines and calls a direct function.

The Code is as follows: Copy code


Var f = (function (x) {return x * x;}) (10 );
Document. write (f );
Var f = (function (x) {return x * x;}) (10 );
Document. write (f );


Result: 100

If a function returns a value, you can use a variable to receive it.

Evaluate m + (m + 1) + (m + 2) +... + (n-1) + n:

The Code is as follows: Copy code

<Html>
<Head>
<Title> evaluate the value of m + (m + 1) + (m + 2) +... + (n-1) + n </title>
</Head>
<Body>
<Script language = "JavaScript" type = "text/javascript">
Function getTotal (m, n ){
Var total = 0;
If (m> = n ){
Return false; // n must be greater than m; otherwise, it is meaningless.
}
For (var I = m; I <= n; I ++ ){
Total + = I;
}
Return total;
}
</Script>
<P onclick = "alert (getTotal (1,100);"> display the value of 1 + 2 + 3 +... + 99 + 100 </p>
</Body>
</Html>

Save and run the code. Click the text to display 5050.

In applications, we generally do this.

1. In HTML, you can use "javascript:" To Call JavaScript Functions or methods, as shown below:

The Code is as follows: Copy code
<Html>
<Head>
<Title> use "javascript:" </title>
</Head>
<Body>
<A href = "javascript: alert ('click this hyperlink ')"> click me </a>
</Body>
</Html>

Ii. "javascript:" You can call not only JavaScript methods, but also user-defined functions, as shown in the following code:

The Code is as follows: Copy code
<Html>
<Head>
<Title> use "javascript:" </title>
<Script language = "javascript" type = "text/javascript">
<! --
Function OnclickLink ()
{
Alert ("You clicked this button ");
}
-->
</Script>
</Head>
<Body>
<A href = "javascript: OnclickLink ()"> click me </a>
</Body>
</Html>

3. Integration with events

The Code is as follows: Copy code
<Html>
<Head>
<Title> integration with events </title>
<Script language = "javascript" type = "text/javascript">
<! --
Function OnMouseOverLink ()
{
Alert ("Move your mouse over the first HYPERLINK ");
}
-->
</Script>
</Head>
<Body>
<A href = "#" onmouseover = "OnMouseOverLink ()"> place the cursor over it. </a> <br>
<A href = "#" onclick = "javascript: alert ('You clicked the second hyperlink ')"> click me </a>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.