In JavaScript, a function is a type of object, which means that it can be passed as a parameter to other functions; in addition, functions can also provide scope.
Basic part of js function: javascript study notes (4) function function part
Syntax for creating functions
Named function expression
Function expression
Declaration of function
A function expression should always use a trailing semicolon, and a trailing semicolon is not required in function declarations.
Function declarations and expressions
Hoisting of functions
The behavior of function declarations is not equivalent to that of named function expressions. The difference lies in the hoisting behavior. See the following example:
All variables, no matter where they are declared in the function body, are internally hoisted to the top of the function. The reason why it is universally applicable to functions is that functions are just objects assigned to variables.
Ascension, as the name suggests, is to lift things below to the top. In JS, it is to promote things (variables or functions) defined at the end to be defined at the front. As can be seen from the above example, foo and bar inside the function hoist are moved to the top, thus covering the global foo and bar functions. The difference between local functions bar and foo is that foo is promoted to the top and can run normally, while the definition of bar() is not promoted, only its declaration is promoted, so when bar() is executed, the result is displayed as undefined instead of being used as a function.
Instant function mode
Functions are also objects, so they can serve as return values. The advantage of using a self-executing function is to directly declare an anonymous function and use it immediately, eliminating the need to define a function that is used once and then not using it, and avoiding the problem of naming conflicts. There is no concept of namespace in js, so it is easy for function name conflicts to occur. In the event of a naming conflict, the last one declared shall prevail.
Mode 1:
Mode 2: Pointing to self-executing function variables
Mode 3: Nested functions
Mode 4: The self-executing function assigns its return value to the variable
Mode 5: The function executes itself internally, recursively
Callback Mode
Callback function: When you pass a function write() as a parameter to another function call(), then call() may execute (or call) write() at a certain moment. In this case, write() is called a callback function.
Asynchronous event listener
The callback pattern has many uses. For example, when attaching an event listener to an element on the page, it actually provides a pointer to a callback function that will be called when the event occurs. Such as:
The above code example shows that the document click event is passed to the callback function console.log() in bubbling mode
Javascript is particularly suitable for event-driven programming because the callback mode supports the program to run asynchronously.
Timeout
Another example of using the callback mode is when using the timeout methods provided by the browser's window object: setTimeout() and setInterval(), such as:
Callback mode in the library
When designing a js library, callback functions will come in handy. The code of a library should use reusable code as much as possible, and callbacks can help achieve this generalization. When we design a huge js library, in fact, users will not need most of the functions, and we can focus on the core functions and provide callback functions in "hook form", which will make it easier for us to build, Extensions, and custom library methods
Curry
Curry technology is a technology that converts a function into a new simplified (making it accept fewer parameters) function by filling multiple parameters into the function body. ————【Proficient in JavaScript】
Simply put, Currying is a conversion process, that is, the process in which we perform function conversion. Example below:
Now, we will be able to use the general methods of any function curry, such as:
When to use currying
When it is found that the same function is being called, and the parameters passed are overwhelmingly the same, then the function may be a good candidate parameter for currying