js function


1. Declaration  and call of
function _ _ Function name (value of parameter 1, value of parameter 2, ...); Documentation Notes: 2) Event call: In HTML tags, use event name="function name ()" 2. Notes on functions : 1) The function name must conform to the small camel case rule, the first letter is lowercase, and then the first letter of every word is uppercase; 2) The parentheses after the function name can have parameters or no parameters, which are called parameterized functions and non-parameterized functions respectively. 3) The parameter list when declaring the function is called the formal parameter list, the formal parameter. (The name of the variable) The parameter list when the function is called, called the argument list, the actual parameters. (Assignment of variables) 4) The number of formal parameter lists of a function has no actual relationship with the number of actual parameter lists. The number of function arguments, depending on the argument list. If the number of the actual parameter list < the formal parameter list, the unassigned formal parameter will be undefined.


















5) The function can have a return value, use return to return the result When
calling a function, you can use a variable to receive the return result of the function. If the function has no return value, the received return value is undefined.

6) Scope of variables in a function:
Variables declared with var in a function are local variables, which can only be accessed inside the function. Variables declared without var are all variables and can also be accessed outside the function.

The parameter list of a function defaults to the local variables of the function and can only be used inside the function.
7) The declaration of the function and the call of the function are not used in sequence. That is, functions can be called before they are declared.
2. Declaration and use of
anonymous functions 1. Declare an anonymous function and assign it directly to an event;

2. Use anonymous function expressions. Assign an anonymous function to a variable.
Declaration: var func=function(){ }
Call: func();


Note: When using anonymous function expressions, the calling statement of the function must be placed after the function declaration statement! ! ! (Difference from ordinary functions)
[Execution sequence of js code]
When js code is running, it will be divided into two parts. Check the loading and execution phases. Check
the .
The following code is an example:


Reason: The execution order of the above code is:
---------------Check the loading phase---------------
var num;
function func1(){};
function func2;


---------------Execution stage-------------
console.log(num);
func1() ;
func2()=function(){};

3. Self-executing function:
1) You can use a variety of operators at the beginning but generally!

! function(formal parameter list){}(actual parameter list)
2) Use () to wrap the function and the parentheses after the function
(function(){}());
3) Wrap the function value with the () value
(function(){})();

Three writing features:
1) Use! At the beginning, the structure is clear, and it is not easy to be confused. It is recommended to use it;
2) It can integrate the anonymous function and the called () into a whole, which is officially recommended;
3) It cannot indicate the integrity of the function and the following (), which is not recommended.

Third, the internal properties of the function
1.Arguments object
1) Function: used to store all the actual parameters when calling the function.
>>>When calling a function and assigning a value with an actual parameter, the parameter list has actually been saved in the arguments array, which can be called in the function using the form of arguments[n], n starts from 0.
2) The number of arguments array, depends on the actual parameter list, has nothing to do with the formal parameters. However, once the formal parameters, actual parameters, and arguments at the nth position exist, the formal parameters will be bound to the arguments and change synchronously. That is, in a function, modify the value of the formal parameters, the arguments will also change, and vice versa.
3) arguments.callee is an important attribute of arguments. Indicates the reference address of the function where arguments are located;
inside the function, you can use arguments.callee() to call the function itself.
Inside a function, the way to call the function itself is called recursion .
Recursion is divided into two parts: recursion and return. With the recursive call statement as the boundary, the function can be divided into upper and lower parts.
Pass : When the function executes the first half. When encountering its own call statement, continue to enter the inner function, and then execute the first half. until the innermost function is executed.
Return : After the innermost function is executed, start from the innermost function and gradually execute the lower part of the function.

When the outermost function is executed, if it encounters its own calling statement, it will enter the inner function to execute, and the second half of the outer function will not be executed. Until the innermost function is executed, it will be executed gradually outwards.

location object
Get the URL address information of the browser;
complete URL path:
protocol name: //host name (ip address): port number/path where the file is located? Pass parameters (name1=value1&name2=value2) #anchor
For example :
http://127.0.0.1:8080/wenjian/index.html?name=jredu#top

 
Other ways to use the method provided by location to jump to the page

history: browsing history

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325129779&siteId=291194637