Javascript beginners Article 5 JS functions page 1/2

Source: Internet
Author: User

1. function:
Function is a JS that is defined once but can be called multiple times. Code .
When a function is called by an object, this function is called the object method.

Function cssrain (x, y)
{
// Code
}
Explanation:
Cssrain: The function name;
(): Operator;
X, Y: the parameter;

2. function return value:
Function a (x ){
Document. Write (X );
}
Function B (y ){
Document. Write (y );
Return y;
}
Alert (A (1) // returns undefined because no return is written.
Alert (B (2 ))

3. Function statement and function quantity:
Function f (x) {return x * X ;}//

VaR F = function (x) {return x * X ;}//

The first is the function statement, and the second is to define an expression directly using the function. Of course, this method is used to create an anonymous function.
Although the direct quantity can be anonymous, you can also specify the function name;
For example:

VaR F = function fact (x) {return x * fact (x-1);} // the benefit of doing so; the call itself is very nice.

4. Function Name:
Function like_this (){}
Or function likethis () {}// camper

5. function parameters:
Because Javascript is a loose type language, you do not need to specify any data types for parameters. Parameters can be more or less,
For example: Function x (a, B) {}// we wrote two parameters.
If we pass three parameters, JS will automatically ignore multiple/
Instance:
Function x (a, B ){
Document. Write (a + "" + B );
}
X (1, 2, 3, 4 );

What happens if we pass only one parameter?
Function x (a, B ){
Document. Write (a + "" + B );
}
X (1 );
We found that undefined is output, so JS will assign less to undefined;
This may causeProgramError.
Solution:
Function x (a, B ){
VaR B = B | ""; // This is or operator. If the first B is undefined, that is, false, it will take the NULL Character
Document. Write (a + "" + B );
}
X (1 );

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.