我如何访问类中函数内的类方法和属性

时间:2019-09-10 09:20:40

标签: typescript function static public-method

我有一个包含函数的类,所以我希望能够在函数内部调用一个类方法

我想找到一种方法来访问send函数内部的类方法file和属性worker_fn吗?

class Upload {
  * uploadFiles(files: FileList | File[]) {
    let file;
    for (let i = 0; i < files.length; i++) {
      if (filesAdded === 1) {
        break;
      }
      file = files[i];
      if (this.types.indexOf(file.type) > -1) {
        filesAdded++;
      } else {
        this._showBadExtensionError();
        return false;
      }
    }
    var worker_fn = () => {
      this.send(file); // i want to able to access send() method and file attribute above 
      self.postMessage('upload sucessfully...');
    };
    var blob = new Blob(["onmessage =" + worker_fn.toString()], {
      type: "text/javascript"
    });

    var worker = new Worker(window.URL.createObjectURL(blob));

  }
  send(file: File, url) {
    let formData = new FormData();
    formData.append('file', file, file.name);
    const response = await this.apiRequest.POST(url, formData);
    return response.json();
  }

}

0 个答案:

没有答案