【问题标题】:How to properly choose between old, deprecated and non-deprecated methods?如何在旧的、已弃用的和未弃用的方法之间正确选择?
【发布时间】:2014-07-23 16:14:31
【问题描述】:

我正在为创建 AudioContext 的一段代码制作补丁,然后想要创建 WebAudio API。具体来说,

function(...) {
  var AudioContext = AudioContext || webkitAudioContext;

  return {

    /* ... */

    init: function() {
      var self = this;

      this.ac = new AudioContext();
      /* createJavaScriptNode is deprecated, doesn't exist anymore in newer Chromium, */
      /* must use createScriptProcessor there. */
      this.anode = this.ac.createJavaScriptNode(this.bufferSize, this.inputChannels,
                                                 this.outputChannels);
      /* ... */
    },
    /* ... */
  };
  /* ... */
};

我们的想法是支持实现了已弃用 API 但尚不支持较新 API 的浏览器。

AudioContextwebkitAudioContext 之间进行选择不是问题,因为它们都是构造函数。

我知道我不能使用相同的习语在createJavaScriptNodecreateScriptProcessor 之间进行选择,因为它们都是方法。 如何在没有太多代码膨胀的情况下最好地做到这一点,即创建另一个局部变量并使用 bind()?

我在想:

this.anode = (this.ac.createJavaScriptNode || this.ac.createScriptProcessor)(this.bufferSize, this.inputChannels,
                                                     this.outputChannels);

我还没有尝试过,因为我担心这种调用方法的方式可能会丢失上下文。 有什么想法或建议吗?

【问题讨论】:

    标签: javascript methods binding this deprecated


    【解决方案1】:

    使用 Chris Wilson 的猴子补丁:https://github.com/cwilso/AudioContext-MonkeyPatch

    这应该为您处理大多数(所有?)情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      相关资源
      最近更新 更多