用参数调用javascript匿名函数

时间:2015-05-03 17:16:59

标签: javascript jquery dropzone.js

有人可以在匿名函数调用中解释参数e。我无法理解匿名函数如何接受参数(下面代码中的第二行)。此代码取自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);

这是如何分配给onprogress

 progressObj = (_ref = xhr.upload) != null ? _ref : xhr;
  progressObj.onprogress = updateProgress;

然后它被称为

    updateProgress();

1 个答案:

答案 0 :(得分:0)

无论调用 updateProgress会传递一个参数,然后可以通过e引用。

示例:

updateProgress(42)e现在会引用42

很可能updateProgress将用作事件处理程序,因此e将引用事件对象。