MY CONCERN
I have been exploring AJAX fundamentals from various online sources. While grasping the multi-step process of sending an asynchronous HTTP request, I noticed a slight discrepancy in the way the usage of the .onload
property is handled on the XHR request object, either being assigned to 1) an anonymous function or 2) a callback (as far as I interpret from MDN).
1ST METHOD
The .onload
property is linked to the ourRequest
object and is assigned to an anonymous function:
ourRequest.onload = function() {
// code implementation here
}
2ND METHOD
The .onload
property is attached to the asyncRequestObject
object and is set to refer to a function by its name (a callback?):
function handleSuccess () {
// code implementation here
}
asyncRequestObject.onload = handleSuccess;
MY QUERY
What distinguishes the functioning of the 1st and 2nd methods?
Additionally, is there a specific rationale behind preferring the 1st method over the 2nd?