Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction of callback example of js function

Shulou Source: shulou.com Published: 2022-06-03 16:55:59 09月20日 Update

This article introduces the relevant knowledge of "callback example introduction of js function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In normal front-end development work, function callbacks are used in many places when writing js.

The simplest example is:

Function doSomething (callback) {if (typeof callback = = "function") {callback ();}} function foo () {alert ("I am a function executed after callback");} doSomething (foo); / * correct * / doSomething (function () {alert ("I am a function executed after callback");}); / * correct * / doSomething ("foo"); / * this will not work, passing in a string, not a function name * /

The above can only be called back with no parameters (divided by the parameters of the callback function that you know in advance). If the function has an unknown function, it cannot be called so easily.

Advanced methods:

1. Use the call method of javascript

Function doSomething (callback,arg1,arg2) {callback.call (this,arg1,arg2);} function foo (arg1,arg2) {alert (arg1+ ":" + arg2);} doSomething (foo,1,2); / * 1:2 * /

2. Use the apply method of javascript

Function doSomething (callback,args) {callback.apply (window,args);} function foo (arg1,arg2) {alert (arg1+ ":" + arg2);} doSomething (foo, [1je 2Jo 3]); / * 1:2 pops up * /

It can be seen that call is basically the same as apply, except that call can only pass parameters one by one, and apply can only pass parameters in the array.

Their first argument is scoped. For example, this is passed above, which means that it is the same scope as the doSomething function. Of course, you can also pass window to indicate the scope of the entire window.

3. Ingenious use of apply

Apply can also be thought of as the execution function of a function, that is, the function used to execute a function. So you will find that sometimes a lot of complicated things become so simple when you make good use of apply.

For example, the push method of an array uses apply to call:

Var arr1= [1,3,4]

Var arr2= [3,4,5]

If we are going to expand the arr2, and then append it to the arr1 one by one, and finally let arr1=.

Arr1.push (arr2) is obviously not good. Because if you do so, you will get [1, 3, 4, 4, 5].

We can only use a loop to push (of course, we can also use arr1.concat (arr2), but the concat method does not change the arr1 itself)

Var arrLen=arr2.lengthfor (var iTuno Bandi)

Tags: Function that is page object array parameter character string method function project selection two code loop output number face work instance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Huawei Docker Shulou Information