In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.