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