"declaration format for functions"
1. Declaration format for functions:
function name (parameter 1, parameter 2, ...) {
function Body Code
return value;
}
call to function:
① Call directly: the function name (the value of parameter 1, the value of parameter 2,....);
② time Call mode: directly in the HTML tag, using the event name = "function name ()"
<button ondblclick= "saysth (' hahaha ', ' yellow ')" > click the button to print the content </button>
2. Considerations for Functions:
① function names must conform to the small hump rule (first letter lowercase, followed by the first letter of each word)
② function name after the (), can have parameters, or can have no parameters, respectively, called the parameter function and the parameterless function;
③ A list of parameters when declaring a function, called a formal parameter list, formal parameters. (The name of the variable)
function Saysth (str,color) {}
The calling function is the parameter list, called the argument list, the actual parameter. (Assignment of variables)
saysth ("hello!!! "," Red ");
There is no actual association between the number of formal parameter lists of the ④ function and the number of argument lists.
the number of function arguments, depending on the argument list.
if the number of arguments list < parameter list, then the parameter is an assignment, which will be undefind.
The ⑤ function can have a return value and return the result using return.
when calling a function, you can use a variable to accept the return result of the function. If the function does not return a value, the accepted result is undefined;
function Fun () {
return "haha";
}
var num = func (); num= "haha"
Scope of variables in the ⑥ function:
In a function, a variable declared with VAR, a local variable, can only be accessed within the function;
variables declared without Var, for all variables, can also be accessed outside the function;
The parameter list of the function, which defaults to the function's lift local variable, can only be used inside the function.
The declaration of the ⑦ function and the invocation of the function are not successively divided. That is, you can call the function before declaring the function.
Fun ();//declaration before the call can be.
function Fun () {}
"Declaration of anonymous functions use"
1. Declare an anonymous function that assigns a value directly to an event.
window.onload= function () {}
2. Use an anonymous function expression. Assign the anonymous function to a variable.
Disclaimer: Var func=function () {}
Call: Func ();
Note : When using an anonymous function expression, the function's invocation must be placed after the function declaration statement!!!
(the difference from a normal function)
3. Self-practising function:
①!function () {} (); You can use a variety of operators with the beginning, general use!
!function (formal parameter list) {} (argument list);
② (function () {} ()); use () to wrap the parentheses after the function set function.
③ (function () {}) (); use the () Value wrap function section;
three ways of writing :
① use! The opening structure is clear, not easy to confuse, recommended use;
② can indicate that anonymous functions and calls () are a whole, the official recommended use;
③ cannot represent the integrity of the function and the post-section (), it is not recommended;
"function
1.Arguments object
① effect: Used to store all arguments when the function was called.
when we call a function and assign a value using an argument, it is actually called in the arguments array, in the function, in the form of arguments[n]. N starting from 0. The number of
②arguments arrays, depending on the argument list, regardless of the parameter.
However, once the parameters, arguments, and arguments of the nth position exist, the shape arguments is bound to the arguments and the synchronization changes. The
③arguments.callee is an important sign of arguments. Represents the reference address of the function where the arguments is located;
Recursive
Inside the function, the invocation of the function itself is called recursion.
Recursion is divided into two parts: recursion and attribution. The function can be divided into the upper and lower parts by using recursive call statements as bounds.
Pass: When the function executes the upper part, when it encounters its own statement, it continues into the inner layer function, executing the upper half. Until you do, wow, press the inner function.
Return: When the inner layer function executes in the back, the lower half of the function is gradually executed starting from the most inner function.
When the outermost function executes, it encounters its own invocation statement, which goes into the inner function execution, while the second part of the outer function is temporarily not executed.
Until the inner function finishes executing, it is executed progressively outward.
"sequence of execution of JS code"
The JS code is divided into two parts at run time: check the load and execution phase
Check the load stage : The syntax error of the code is detected first, and the declaration of the variable and function is performed.
Execution Phase : The variable has the value of the assignment, the function calls, etc., all belong to the execution phase.
take a look at the code for example:
console.log (num); undefine
var num=10;
func1 (); function can execute normally
function Func1 () {}
Func2 (); function cannot be executed, print Func2, display undefine
var func2= function () {}
-----------Check the load stage----------
var num;
var func1 = function () {};
var Func2;
----------------------------------------
-----------Code Execution Phase----------
Console.log (num);
var num = 10;
Func1 ();
Func2 ();
Func2 = function () {}
----------------------------------------
Console.log (num); undefine
var num=10;
Func1 (); function can execute normally
function Func1 () {}
Func2 (); function cannot be executed, print Func2, display undefine
var func2= function () {}
JS Learning Three (function)