Micro letter applet common typeof cb == "function" && cb (that.globalData.userInfo)

Letter applet official demo as well as a lot of the code will often appear in the function cb typeof   " " && cb (that.globalData.userInfo) sentence. I do not understand the beginning, most of the online answer is scanty, check the code official api with the demo, and finally became known.
Meaning of the code itself is not a judge cb is a function of the type of parameters passed at the same time, a function called cb, so look seems still do not understand, then add the source point of view.

1. The following is the official user information acquired demo function definition
getUserInfo: function (CB) {
the console.log ( 'getUserInfo function begins execution');
var that the this =
IF (this.globalData.userInfo) {
typeof CB == " function "&& CB (this.globalData.userInfo)
} the else {
// call the login interfaces
wx.login ({
Success: function () {
wx.getUserInfo ({
Success: function (RES) {
the console.log ( 'user data acquisition success');
that.globalData.userInfo = res.userInfo
typeof CB == "function" && CB (that.globalData.userInfo)
}
})
}
})
}
}

2. The following is the code in function onLoad index.js the
onLoad: function () {
the console.log ( 'begins execution function onLoad')
var = that the this
// call the application example of the method for acquiring the global data
app.getUserInfo (function (the userInfo) {
// update data
that.setData ({
the userInfo: the userInfo
})
the console.log ( 'user data is stored in the current page ");
})
}

Explanation: getUserInfo definition method, the received parameter called cb, the use of time is to get the user information in time, if this type of function is executed cb cb This function is called. Look at the function call, call this function in the onLoad method of index.js and define the data content as a function parameter, the function is to set the incoming userInfo in the current page.

Steps to follow: when entering the index page first calls the onLoad function, and then executes app.getUserInfo () function (output can start almost a little), in getUserInfo () function will first determine whether the local user information is stored, the first when the first performance certainly did not go else, perform the login method to get the user information and then performs typeof cb == "function" && cb (that.globalData.userInfo) to perform the method as a function of parameters, according to the output information is one such walk a process to output 'onLoad method to begin', then 'getUserInfo function started', then to 'user data acquisition success' final output will be 'user data into the current page'.

Tags:

Guess you like

Origin www.cnblogs.com/gzhbk/p/12124838.html