Home >WeChat Applet >Mini Program Development >Basics of WeChat Mini Program Development: Callback Functions, Anonymous Functions, and Closures (4)
Strictly speaking, this is not a WeChat applet tutorial, but I will use the app.js code in the previous article as an example, so let’s just make it count.
The callback function may be a little difficult to understand for students who are new to programming. After all, the use of callback functions is contrary to the intuitive process of program sequence execution.
Imagine that you have ordered a takeout. One way is to check whether the takeout has arrived regularly. The other way is to show your phone number to the delivery person and call you when it arrives.
It is easy to see that the second solution is more efficient. In fact, when this notification mechanism is applied to the programming field, it is callback function.
Students who are familiar with win32 development should know that a typical windows program framework is a message loop plus a window procedure function. The windows system takes over the message acceptance, and then calls the developer's window procedure function to complete the specific message processing logic. The window procedure function is a callback function.
Take the above win32 program as an example. We know that for security reasons, the Windows operating system does not allow developers to directly access hardware resources. Microsoft developers provide an API to handle the message loop, but the response logic for specific messages needs to be provided by the developer. In this case, the callback function is a good implementation solution.
To give another example, imagine that you are involved in the development of a mobile phone device management software project, and you are responsible for the underlying device communication module. When the user inserts the device into the computer, you need to notify the upper-level module to process it. For the sake of flexibility and versatility, it is impossible for you to put the processing logic when the device is connected in the module you are responsible for. At this time, the upper-level caller can provide a callback function, and your module calls the callback function when the device is connected. That’s it.
Regarding callback functions, there is a so-called Hollywood criterion: Don't call me; I'll call you!
In c, c++ and other languages, when you need to use a callback function, you need to pre-define a function body. The callback function is usually only provided to other modules for calls. In order to simplify coding, subsequent scripting languages such as JavaScript provide support for anonymous functions. (Note: The new C++ standard has also begun to support anonymous functions, called Lambda functions)
getUserInfo:function(cb){ var that = this if(this.globalData.userInfo){ typeof cb == "function" && cb(this.globalData.userInfo) }else{ //调用登录接口 wx.login({ success: function () { wx.getUserInfo({ success: function (res) { that.globalData.userInfo = res.userInfo typeof cb == "function" && cb(that.globalData.userInfo) } }) } }) } },
The above code comes from app.js in the previous tutorial. When calling wx.login, a The anonymous function performs logical processing after the call is successful, which is the part after success. You can see that there is only a function definition but no function name, so except as a callback function, the function cannot be called elsewhere.
In fact, anonymous functions are just a coding simplification, but the benefits they bring are not just reduced coding.
In programming technology, closure should be a more advanced technology.
When using callback functions, it usually involves the passing of some context. In languages such as c/c++, global variables or heap memory are used to pass context. The disadvantages of global variables are obvious, and heap memory is prone to memory leaks. In more advanced scripting languages, context transfer can be easily accomplished through closure technology.
Taking the above code as an example, that.globalData.userInfo = res.userInfo is executed in the callback function to save user information. The that variable is assigned by var that = this, so the variable points to the app object itself. , so the user information can be saved successfully.
We can see that the that object is a variable on the getUserInfo method stack. Without closure technology, the anonymous callback function here cannot directly use the that variable, so the app object needs to be passed to the callback function ( Global variables or function parameters), and with the support of closure technology, the callback function can access that variable just like using internal variables of the function, which is much more convenient in syntax.
The above is the detailed content of Basics of WeChat Mini Program Development: Callback Functions, Anonymous Functions, and Closures (4). For more information, please follow other related articles on the PHP Chinese website!