Home > Article > Web Front-end > Detailed explanation of function pattern in JavaScript_javascript skills
The role of JavaScript design patterns is to improve the reusability and readability of code, making it easier to maintain and expand the code
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.
Syntax for creating functions
Named function expression
Declaration of function
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:
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 4: The self-executing function assigns its return value to a variable
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:
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:
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 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