Specific knowledge of JS functions (a must-see for beginners)

JS function specific knowledge

1. Introduction to function:
What is a function? A function is a encapsulated code block, or a function. In our program, most of the time we are block-oriented programming. A complete program consists of multiple Composed of blocks. A block is a function. When we need a function, we can call this function, because the function can be called multiple times, which greatly reduces the amount of code, is more convenient, and easier to maintain. For example, if we require a maximum value, we can write a maximum value For the value function function, we only need to pass the parameters and call to get the maximum value. js also has many built-in functions, which are all functions written by developers and encapsulated. We only need to understand how to call them.
2. The basic way to define a function: define it
through the function keyword, followed by the function name, brackets (), and curly brackets {}, which can have multiple parameters, depending on the actual situation.
Syntax:
function function name (parameter 1, parameter 2,...) { code block to be executed (function body) }; specific code: 3. Function call: there are three ways to call a function: function name (); inside the brackets can be Passing parameters a. When an event occurs (when the user clicks the button) b. When the JS code is called c. The function is automatically called after the function is created a. Write a function and call it. The function of the function is to output a 99 multiplication table. Operation result: b. The function calls itself after execution . c. Bind the button, which is triggered when the user clicks the button.



Insert picture description here






Call with function name

Insert picture description here
Call yourself after the function is executed

Insert picture description here
**4. Pass the parameters of the function, ** Pass the parameters in the parentheses after the function name. There is a formal parameter and an actual parameter. You must be clear. The formal parameter is the value of the actual parameter. The formal parameter is the parameter we pass in. The formal parameter is used as a formal parameter, which can receive different incoming parameters. See Code, I have commented that one is a formal parameter, and that is an actual parameter.
Insert picture description here
**5. The return value of the function: **Returns the execution result, which will not be displayed on the page. Variables are needed to receive the function. When the function encounters a return, the result will be returned immediately and the function will be aborted.
Insert picture description here
6. Calling a function in a function (calling other functions in the function) Of course, there is also a recursive call, which will be discussed below.
For details, please see the code:
Insert picture description here
7. Local variables and global variables: (Note that if the function is not called, the assignment inside is unsuccessful)
1. Variables declared outside the function are global variables and can be called in the entire js document ;
2. Variables declared with var inside the function are local variables and can only be called inside the function; (Local variables, deleted from the memory after the function is executed, can be declared with local variables, don’t use global variables, which can improve efficiency ;)
3. There is no var declaration inside the function, and the variable declared by the direct assignment method is a global variable;
8. Assign an anonymous function to a variable.
Note: The call statement of the function must be placed after the function declaration statement.
Insert picture description here
9. Self-calling of anonymous functions.

Insert picture description here
10. Recursive call of
function : A function calling itself in its function body is called recursive call, and this kind of function is called recursive function. The execution of a recursive function will call itself repeatedly, and enter a new layer each time it is called. When the innermost function is executed, it will exit from the inside to the outside layer by layer (when using recursion, you must give a Recursive end condition, because the stack memory space is limited, if the stack memory is full and cannot be popped, the program will report an error).
Principle: Insert picture description hereUse recursion to achieve the sum from 1 to 100.
Insert picture description here
10. Callback function:
From the literal sense, the callback function is to pass a parameterized function, that is, pass this function as a parameter to another main function, and then execute the passed in after the main function is executed. Functions as parameters. The parameterized function that goes through this process is called a callback function. In other words, the function that is passed as a parameter to another function (the main function) is called a callback function (pass the function name as a parameter to another function, and then call it in the function that is passed in).Insert picture description here

11. Commonly used built-in functions:
These functions are set by the developer. We don't need to know the underlying code, just know what the function of this function is and call it directly. The values ​​in brackets are the values ​​that need to be converted. Of course, there are many built-in functions. You can download a JS API document, and check the specific function when you need it.

1. Number() is converted to numeric type.
2. String() is converted to string type.
3. Boolean() is converted to Boolean type.
4.parseInt() converts the string to an integer.

Guess you like

Origin blog.csdn.net/m0_46188681/article/details/106089749