Home > front end >  JavaScript object to summarize
JavaScript object to summarize

Time:09-21

What is the function
Function: put a tuo repetitive code encapsulation, in need of a direct call to
Function function
- function is also an object
- function can be encapsulated in some function (code), the need to use to perform these functions (code)
- function can save some code called when need
- use typeof inspect a function object, returns the function
The definition of the function:
Grammar:
The function () function name {
Function body -- -- -- -- -- a tuo repetitive code
}
Have the function, write these reusable code in the function, in need of direct call function can
1. The parameters of the function
Define one to two Numbers and the function of
Can be in () function to specify one or more parameters (formal parameters)
Use ", "between multiple parameters, and declare parameter is equivalent to the function variables declared within the corresponding
But does not assign
Note: call a function when the parser does not check the type of argument, so pay attention to, is it possible to received illegal parameters, if possible you will need to test types of parameters for
When calling a function, the parser will not to check the number of arguments
Redundant parameters will not be assignment
If the argument is less than the parameter data, there is no corresponding argument joining is undefined
Parameter: function in defining the variables in the parentheses call parameter
Argument: the function is invoked when incoming values in parentheses are called arguments, arguments can be variables can also be values
For example:




2. The return value of a function with immediate execution
Set: setting
Get: retrieve
JavaScript function return values
Create a function that is used to calculate the number of three and
Can return to return the function return value
Grammar: return value; After the return value will be as a result of execution of returns,
You can define a variable to receive the function return values
In the function after the return statement will not execute
If don't talk to any value, after return statement is equivalent to return an undefined
The immediate execution function
Function definitions, are called immediately, this function is called the immediate execution function
The executive function often perform a

For example:





  • Related