1, Function:
function is a js code that is defined once but can be called multiple times.
When a function is called by an object, then the function is called a method of the object.
1 2 3 4 |
|
Explanation:
cssrain : is the function name;
() : is the operator;
x, y : is the parameter;
2, the return value of the function:
1 2 3 4 5 6 7 8 9 |
|
3 function statement and function literal:
1 2 |
|
The first one is created by the function statement, The second one is to directly define an expression using a function literal. Of course, in this way, an anonymous function is created.
Although the direct variable can be anonymous, you can also specify a function name;
For example:
1 |
|
4 Function naming:
function like_this(){}
or function likeThis(){} // Camel case
5 Function parameters:
Since js is a loosely typed language, the parameters do not need to specify any data type. Parameters can be more or less,
For example: function x(a,b){} //We wrote 2 parameters
If we pass 3 parameters, js will automatically ignore the extra ones/
Example:
1 2 3 4 |
|
What happens if we only pass one parameter?
1 2 3 4 |
|
We found that undefined was output, so js will assign the less to undefined;
This may cause program errors.
Solution:
1 2 3 4 5 |
|