I. Declaration and invocation of functions
1, the function of the declaration format:
Function name (parameter 1, parameter 2, ...) {//Function body code return value;}
2, the function of note:
①function names must conform to the small hump rule!! (The first letter is lowercase, then the first letter of each word is capitalized); the () after the ② function name, you can have parameters or no parameters. is called the parameter function and the parameterless function respectively; ③
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 argument list when the function is called is called an argument list , the actual argument. (Assignment of variables)Saysth ("Ha ~ ha ~ ~ ~ ~ ~", "Blue"); ④
There is no actual association between the number of parameter lists in the function and the number of argument lists. The number of function arguments, depending on the argument list. IfThe number of argument lists < parameter list is the parameter of the assignment, which will be undefined. ⑤
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. Iffunction does not return a value, the accepted result is undefined;
function func () {
return "haha";} var num = func ();//num = haha
document.write (num);
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 are global variables and can be accessed outside of the function;The formal parameter list of the function, which defaults to the local variable of the function and can only be used inside the function.
The declaration of the ⑦ function has no precedence over the invocation of the function, and the function can be called before the function is declared. Func (); It is also possible to declare func before the call; function func () {}
second, the function of the call:
① Call directly: function name (value of parameter 1, value of parameter 2, ...);
② Event invocation: In HTML tags, use event name = "function name ()"
<onclick= "Saysth (' Demon Demon ', ' Black ')"> click button to print content </ Button >
/** Document comments: Start with two *. Written above the function, you can see the description in the document when the function is called.*//***STR: What you want to print; * Color: what color you want to display; */
function Saysth (str,color) {document.write ("<style= ' color:" +color+ "; >"+str+"</div>");}
third, the declaration of anonymous functions use
1. Declare an anonymous function and assign the value directly to an event;
Window.onload = function () {}
2. Use an anonymous function expression. Assign the anonymous function to a variable;
var func = function () {}//declares func ();//Call
Note: When using an anonymous function expression, the function's call statement must be placed after the function declaration statement!!! (The difference from the normal function!! )
3. Self-executing function:
①! function () {} ();//You can start with a variety of operations, but generally with an exclamation point (!);! function (formal parameter list) {} (argument list);
! function () { alert (789); } ();
② (function () {} ());//use () enclose the parentheses of functions and functions;
(function (num) { alert (num); } (789));
③ (function () {}) ();//use () value wrap function part;
(function (num) { alert (num); }) (789);
"Three kinds of characters"
① use! Opening, clear structure, not easy to confuse, recommended use;
② can indicate that anonymous functions and calls () are a whole, the official recommended use;
③ cannot indicate the integrity of the function and subsequent (), and is not recommended for use;
Four
, internal properties of the function
1. Arguments Object
① function: Used to store all arguments when a function is called. >>> when a function is called and an argument is assigned a value, the argument list is actually saved to the arguments array, which can be in the function,called using the arguments[n] form. N starting from 0.
the number of ②arguments data depends on the argument list, regardless of the parameter. However, once the nth position of the formal parameters, arguments, arguments are present, the shape arguments in the arguments binding, synchronous change, (that is,Modifying the value of a parameter in a function also changes the arguments. Conversely, it is also established. )
function func (a,b,c,d,e) { alert (arguments[2]); alert (c); c = 1; Alert (arguments[2]); Console.log (arguments); } Func ("haha", 4,5);
③arguments.callee is an important mouse for arguments。 Represents the reference address of the function where the arguments is located; In a function, the function itself can be called using Arguments.callee ().inside a function, calling 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.handed: Executes the upper part of the function and, when it encounters its own invocation statement, proceeds to the inner function and executes the upper half. Know that the most inner function is executed.return: When the inner function is executed, the bottom half of the function is gradually executed, starting with the 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 most inner function is executed, step outward.
/* Recursive and Debug * / var num = 1;function func () { console.log (num); num++; if (num<=4) { Arguments.callee (); } num--; Console.log (num);} Func ();
Five, JS code pairs of execution order problem
JS code is divided into two parts when it is run. Check the loading and execution phase; Check the load stage: The syntax error of the code is checked first, and the declaration of the variable and function is made.implementation Phase: The assignment of variables, the invocation of functions, etc., all belong to the execution phase.
take the following code as an example:
Console.log (num);//undefined var num = 10; Func1 (); function functions normally func1 () {} func2 (); function cannot be executed, undefined var func2 = function () {} is displayed when printing FUNC2: The above code is executed in the following order:--------Check execution phase---------- var num; function Func1 () {} var Func2; --------Code Execution Phase---------- console.log (num); Func1 (); Func2 ();
The function in JS