如何在不将 this 存储在变量中的情况下将 this 上下文传递给自调用匿名函数?

How can I pass `this` context to a self invoking anonymous function without storing `this` in a variable?

理想情况下,我们可以使用 Function.prototype.bind 函数来完成此操作。我认为这里也没有使用粗箭头功能的明确方法。 Ycombinator 魔法?

这是我目前尝试的方法:

(function pump () {
  return browserReadableStreamReader.read().then(({ done, value }) => {
    if (done) {
      return this.end()
    }

    this.write(value)
    return pump()
  })
}).bind(this)()

这是我所做的:

const { PassThrough } = require('stream')
/**
 * Google Chrome ReadableStream PassThrough implementation
 * @extends PassThrough
 */
class BrowserPassThrough extends PassThrough {
  /**
   * @param {Object} options - options to pass to PassThrough
   * @param {ReadableStreamDefaultReader} browserReadableStreamReader - reader
   */
  constructor (options, browserReadableStreamReader) {
    super(options)
    this.reader = browserReadableStreamReader
    this.pump()
  }

  pump () {
    this.reader.read().then(({ done, value }) => {
      if (done) {
        return this.end()
      }

      this.write(value)
      this.pump()
    })
  }
}
module.exports = BrowserPassThrough