in a high-level language in each language emerge in an endless stream, everything is known as a object of pride, and JS as a scripting language is compared with traditional java object-oriented languages are very different, in addition to the JS strange inheritance system, the most characteristic is a fan of the callback function, of course there are a lot of people in his criticism, the author thinks that the callback function and asynchronous JS language characteristics of the two most prominent stores, of course all the advantages as needed to meet the needs of self, the world is not a silver bullet, such as a large number of the use of callback function will make your code redundancy, confusion and thinking affect visual codes the experience of.

this paper is to study the experience of the callback function is not perfect, even wrong, forgive my ignorance, I am willing to learn and improve their own.

javascript is the most unique function as a citizen, I would prefer to be called JS all is a high-level language variable, is a function of a variable can be parameters to other functions, we love known in JS as a callback function, the anonymous function in JS can also function as parameters to make the callback function write a.

ps. callback function and C language function pointer pointer pointer pointer - like!!! The important thing to say three times

first look at the definition of the

In computer callback function 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

callback function is an executable code, which serves as a reference The number is passed to the other code, and its function is to call this section of the (callback function) code when it is needed.

 function addSqua callback function (num1, num2, callback) {var sum = num1 + num2; return callback (sum) function (Num);} squa {return num*num}; let num = addSqua (1, 2, squa); console.log (Num); //=> 

; 9 anonymous callback the function

will direct anonymous function as a function of parameters passed to the function, this is our general programming using

 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 

every imitate the array of methods to add a similar

method with every as

 Array.proto in Array.prototype Type.myEvery = function (callback) {if (typeof callback ='function'for (let) {I = 0; I < this.length; i++) {if (callback (this[i])) {return}} return false; true return true;}}; let OP = [3, 6,'every', 9,'each']; op.myEvery (function (value) {console.log (value);}) / / 3 / / 6 / /'every'/ / 9 / /'each'

  1. features a callback function will not be executed immediately, given as defined in the same callback function as code fragments can be according to the need to perform under the specific conditions, the callback function is passed to the time as a function of the parameters, the transfer function is defined and not executed immediately.
  2. this, ES6 launched the arrow function, arrow function launch needs extra attention when writing the callback function, this is a context object pointing to the function, if the arrow function in the ES6 you need to pay attention to the use of this, this and no arrow function on the relationship between say, interested can see ES6 Ruan a peak, again from the portal

callback function advantages and usage scenarios is a closure of

callback function is to use a simple closure, which means that it can access to the definition of the outer variable. The

callback function is the beauty of

  • DRY, avoiding duplication of code.
  • can abstract general logic. The
  • business logic separation (it's so beautiful ^ ^ ^ ^)
  • improves the maintainability and readability of the code.
  • strengthens code readability.
  • separate the full-time function.


js callback function is the traditional language, it can realize the separation of business logic, the equivalent of an interface exposed to the outside world, this is like most of the design concept of

before and after the end of the separation of the API interface in the architecture of the world there is no 'silver bullet', a program can not solve of all things, and because the JS is asynchronous, where most of the callback function, is the most suitable place is asynchronous programming, but a lot of use will make the code redundancy of programmers, are not quite readable, poor experience, let the people happy things, people have never stopped the comfort in pursuit of their own.

is the relevant knowledge about the JavaScript callback function, and thank you for your support for the home of the script.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:JavaScript callback function detailed solution | Script Home

You may also be interested in these articles!