Can someone explain the parameter e
in the anonymous function call. I am unable to understand how can the anonymous function accept the parameter (the second line in the below code). This code is taken from DropZone
.
updateProgress = (function(_this) {
return function(e) {
var allFilesFinished, progress, _j, _k, _l, _len1, _len2, _len3, _results;
if (e != null) {
progress = 100 * e.loaded / e.total;
for (_j = 0, _len1 = files.length; _j < _len1; _j++) {
file = files[_j];
file.upload = {
progress: progress,
total: e.total,
bytesSent: e.loaded
};
}
} else {
allFilesFinished = true;
progress = 100;
for (_k = 0, _len2 = files.length; _k < _len2; _k++) {
file = files[_k];
if (!(file.upload.progress === 100 && file.upload.bytesSent === file.upload.total)) {
allFilesFinished = false;
}
file.upload.progress = progress;
file.upload.bytesSent = file.upload.total;
}
if (allFilesFinished) {
return;
}
}
_results = [];
for (_l = 0, _len3 = files.length; _l < _len3; _l++) {
file = files[_l];
_results.push(_this.emit("uploadprogress", file, progress, file.upload.bytesSent));
}
return _results;
};
})(this);
this is how it is assigned to the onprogress
progressObj = (_ref = xhr.upload) != null ? _ref : xhr;
progressObj.onprogress = updateProgress;
then it is called as
updateProgress();
The function expressions are just used to create scope. The function that is returned from the IIFE (immediately invoked function expression) is assigned to the updateProgress
varible, so the final result is basically as if you had a regular function with a parameter:
function updateProgress(e) {
var allFilesFinished, ...
...
return _results;
}
When updateProgress
is used as an event handler, it will be called with the event object as the first parameter. That ends up in the e
parameter.
When called without a value, the parameter e
gets the value undefined
. That makes the code execute the else
part, ie showing the progress at 100%.
Whatever calls updateProgress
will pass an argument, which then can be referenced via e
.
Example:
updateProgress(42)
: e
would now refer to 42
.
Most likely updateProgress
will be used as event handler, so e
would refer to the event object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.