Javascript callback function details
In the age of the emergence of advanced languages, each language claims to have a proud saying that everything is an object, js, as a scripting language, is very different from traditional object-oriented languages such as java. In addition to the strange inheritance system of js, the most fascinating feature is the callback function, of course, many people have criticized him. I think callback functions and Asynchronization are the two most prominent stores in js language. Of course, just as all the advantages need to satisfy their own needs, there is no silver bullet in this world. For example, a large number of callback functions will make your code redundant and affect the visual and thinking experience of the Code.
This article is my own experience in learning callback functions. It will inevitably be imperfect or even wrong. Forgive me for my ignorance. I would like to learn and improve myself with an open mind.
The most unique feature of javascript is that a function is a first-class citizen. I prefer to call javascript a high-level language in which everything is a variable. A function is also a variable that can be used as a parameter for other functions, we like calling callback functions in js. In addition, the anonymous functions in js can be used as function parameters to write callback functions like fish.
Ps. The callback function is similar to the function pointer in c-pointer !!! Important thing: three times
Let's take a look at the definition of the callback function.
In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time
A callback function is an executable code segment that is passed to other code as a parameter. Its function is to call this code as needed.
Callback Function
function addSqua(num1, num2, callback){ var sum = num1 + num2; return callback(sum); } function squa(num){ return num*num; } let num = addSqua(1, 2, squa); console.log(num); //=>9
Anonymous callback function
Anonymous functions are passed directly as function parameters to functions, which is commonly used in programming.
function addSqua(num1, num2, callback){ var sum = num1 + num2; return callback(sum);}let num = addSqua(1, 2, function squa(num){ return num*num;}); console.log(num); //=9
Mimic the every method in the array
Add methods similar to every in Array. prototype
Array.prototype.myEvery = function (callback) { if (typeof callback === 'function') { for (let i = 0; i < this.length; i++) { if (callback(this[i])) { return false; } } return true; } return true; } let op = [3, 6, 'every', 9,'each']; op.myEvery(function (value) { console.log(value); }) // 3 // 6 // 'every' // 9 // 'each'
Features of callback Functions
- The callback function is not executed immediately. As shown in the definition, the callback function can be executed as a code snippet under specific conditions as needed. When the callback function is passed to a function as a parameter, only the function definition is passed and will not be executed immediately.
- This, ES6 introduces the arrow function. When the arrow function is launched, you need to pay special attention when writing the callback function. this is the context object pointing to the function, if the arrow function is used in ES6, pay attention to the use of this. In the arrow function, this does not have a context relationship. If you are interested, you can view ES6 Ruan Yifeng and provide the portal again.
The advantages and application scenarios of callback functions are closures.
A callback function is a simple use of a closure, that is, it can access the variables defined in its outer layer.
The beauty of callback Functions
- DRY to avoid repeated code.
- General logic can be abstracted.
- Business Logic separation (it's so beautiful ^-^)
- Improve code maintainability and readability.
- Enhance code readability.
- Separate dedicated functions.
The greatness of the js callback function is that it is not available in other traditional languages. It can implement business logic separation, which is equivalent to exposing an interface to the outside world. This is like the API Interface Design Concept in the frontend and backend separation architecture.
There is no silver bullet in this world, and there is no solution to all things. Because Javascript is generated asynchronously and callback functions are used most often, asynchronous programming is also the most suitable place. However, a large amount of use will make the programmer's code redundant, there is a lot of unreadable rows, the experience is very poor, simple and pleasant, people have never stopped pursuing their own comfort.
The above is the knowledge about the javascript callback function compiled in this small Editor. Thank you for your support for the help house.