JavaScript Getting Started learning Fifth JS function 1th/2 Page _ Basics

Source: Internet
Author: User
Tags anonymous
1, Function:
A function is a JS code that is defined once and can be invoked multiple times.
When a function is called by an object, the function is called the method of the object.

function Cssrain (x, y)
{
Code
}
Explain:
Cssrain: For the function of the name;
(): is an operator;
X, y: as parameter;

2, the return value of the function:
function A (x) {
document.write (x);
}
function B (y) {
document.write (y);
return y;
}
Alert (A (1))///Because no return is written, returns undefined
Alert (b (2))

3 function statements and functions Direct quantity:
function f (x) {return x * x;} //

var f = function (x) {return x * x;} //

The first one is created by a function statement, and the second is to define an expression directly with the direct amount of the functions, which, of course, is created as an anonymous function.
Although the direct volume can be anonymous, it is possible to specify a function name;
Like what:

var f = function fact (x) {return x * fact (x-1);} The benefits of doing so; Call itself very cool.

4 Function Name:
function Like_this () {}
or function Likethis () {}//Hump type

5 Parameters of the function:
Because JS is a relaxed type language, parameters do not need to specify what data type. Parameters can also be more or less,
For example: function x (a,b) {}//We wrote 2 parameters
If we pass 3 parameters, JS automatically ignores many/
Instance:
function x (a,b) {
document.write (A + "" +b);
}
X (1,2,3,4);

What happens if we only pass one argument?
function x (a,b) {
document.write (A + "" +b);
}
X (1);
We found that the output of undefined, so JS will be less, to give undefined;
This may cause program errors.
Solve:
function x (a,b) {
var b = B | |  " "; This is the OR operator, if the previous B is undefined, or false, he takes the trailing null character
document.write (A + "" +b);
}
X (1);
Current 1/2 page 12 Next read the full text
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.