MindMap Gallery JS Basics Day4
This is a mind map about the basics of JS Day 4, including functions, type conversions, etc. It is full of useful information. Friends in need should quickly collect it!
Edited at 2024-01-18 10:22:41This is a mind map about bacteria, and its main contents include: overview, morphology, types, structure, reproduction, distribution, application, and expansion. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about plant asexual reproduction, and its main contents include: concept, spore reproduction, vegetative reproduction, tissue culture, and buds. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about the reproductive development of animals, and its main contents include: insects, frogs, birds, sexual reproduction, and asexual reproduction. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about bacteria, and its main contents include: overview, morphology, types, structure, reproduction, distribution, application, and expansion. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about plant asexual reproduction, and its main contents include: concept, spore reproduction, vegetative reproduction, tissue culture, and buds. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about the reproductive development of animals, and its main contents include: insects, frogs, birds, sexual reproduction, and asexual reproduction. The summary is comprehensive and meticulous, suitable as review materials.
JS Basics Day4
function
Why functions are needed
Function: A function is a block of code designed to execute a specific program
illustrate
Functions can 'wrap' code with the same or similar logic, and execute the wrapped code logic through function calls. The advantage of this is that it streamlines the code and facilitates reuse.
The alert() and prompt() we used before are all JS functions, but they have been encapsulated and we can use them directly.
Function usage
Function declaration syntax: functio function name () {function body}
Function name naming rules
Basically the same as variable naming
Minimize camelCase nomenclature
The prefix should be a verb
Naming suggestions: Common verb conventions
can: Determine whether a program can be executed
has: determine whether it contains a certain value
is: Determine whether it is a certain value
get: Get a certain value
set: set a certain value
load: load some data
Function calling syntax
Function name()
Note: The declared (defined) function must be called before it is actually executed. Use () to call the function
function body
The function body is a component of the function. It is responsible for 'wrapping' corresponding or similar code. The code inside the function body will not be executed until the function is called. The function code of the function must be written in the function body
Function parameter passing
Declaration syntax: function function name (parameter list) {function body}
Calling syntax: function name (passed parameter list)
function function name (formal parameters) {function body} function name (actual parameters)
Note:
Formal parameters: When declaring a function name, what is written in parentheses to the right of the function name is called a formal parameter (formal parameter).
Actual parameters: When calling a function, the ones written in parentheses to the right of the function name are called actual parameters (actual parameters)
The formal parameters can be understood as variables declared within this function (for example, num1 = 10). The actual parameters can be understood as assigning values to this variable.
When developing a summary, try to keep the formal parameters and actual parameter quantities consistent.
The alert ("print") and parselnt ("11") used are essentially parameters passed in function calls.
Parameter default value
Formal parameter: can be regarded as a variable. A variable does not give a value. The default is: undefined
If the user does not enter actual parameters, NaN will appear
It can be improved. If the user does not enter actual parameters, the formal parameters can be given default values. The default value can be 0, so that the program is more rigorous.
The default value will only be executed when no actual parameters are passed, so if there are parameters, the passed actual parameters will be executed first.
function return value
When a function needs to return data, use the return keyword
Syntax return data
functions that return values
Using the return keyword in the function body can transfer the internal execution results to the outside of the function for use
The code after return will not be executed and will end the current function immediately, so the data after return cannot be wrapped and co-written.
The return function can have no return. In this case, the function returns the default value undefined.
advantage :
After the function is executed, the result is obtained, and the result is what the caller wants to get (the function does not need to output the result internally, but returns the result)
The execution results are more scalable and can be used by other programs.
Details added
The latter of two identical functions will overwrite the previous one.
In JS, the actual parameters of the formal parameters can be different.
If there are too many formal parameters, undefined will be automatically filled in.
If there are too many actual parameters, then the extra and the actual parameters will be ignored.
Once the function encounters return, it will not continue to execute. Use return to end the function.
Scope
Generally speaking, the name used in a piece of code is not always valid and available, and the code scope that limits the availability of this name is the scope of this private asset.
Scope improves the locality of program logic, increases program reliability, and reduces name conflicts.
In JS, there are different scopes
Global scope: Acts on the environment where all code is executed (inside the entire script tag) or an independent js file
Local scope: The code environment within the function is the local scope. Because it is related to functions, it is also called function scope.
In JS, according to different scopes, variables can be divided into
Global variables: Global variables can be accessed and modified in any area
Local variables: Local variables can only be accessed and modified within the current function
There is a pitfall in variables, special case: if the variable is not declared inside the function, it can be assigned directly and treated as a global variable, but it is strongly not recommended.
Variable access principles
As long as it is code, there must be at least one scope
Local scope written inside a function
If there is a function within a function, then another scope can be born in this scope.
Access principle: If you can access the local area first, if the local area is not available, then look for the global area.
anonymous function
Functions without names cannot be used directly
Usage
function expression
Assigning an anonymous function value to a variable and calling it through the variable name is called a function expression
Syntax: let fn = function(){function body}
Execute function immediately
Avoid contamination before global variables
Syntax 1: (function(){console.log(11)})()
Syntax 2: (function(){console.log(11)}())
Note:
No need to call, execute immediately, in fact, the essence has already been called
Separate multiple immediately executed functions with semicolons
logical interrupt
Short circuiting in logical operators
Short circuit: only exists in && and ||. When certain conditions are met, the code on the right will not be executed.
&&: If the left side is false, it will be short-circuited.
||: If the left side is true, it will be short-circuited.
Reason: The result of the entire equation can be obtained through the left side, so there is no need to judge the right side
Operation result: Regardless of && or ||, the operation result is the last executed expression, which is generally used in variable assignment.
type conversion
Convert to Boolean type
Show conversion
Syntax: Boolean (content)
Memory: ‘’, 0, undefined, null, false, NaN are all false after being converted to Boolean values, and the rest are true
implicit conversion
There is string addition ‘’ 1’, the result is ‘1’
Subtraction - (like most operations) can only be used in mathematics, it will convert the empty string '' to 0
null will become 0 after digital conversion
undefined will become NaN after digital conversion.