我有一批类似于下面代码的 Web 音频 API 节点。我想将其抽象为一个简单的构造函数,但我遇到了麻烦。我不确定我做错了什么。最终结果应该类似于
function filterTemplate(name,freqVal){
this.name = context.createBiquadFilter();
this.name.type = 5;
this.name.gain.value = null;
this.name.Q.value = 1;
this.name.frequency.value = this.freqVal; // freqVal is here
}
当我调用函数时:
var filter = new filterTemplate("theName",200); //Uncaught TypeError: Cannot call method 'createBiquadFilter' of undefined
我将方法更改为如下所示,错误已删除
this.name = function(){return context.createBiquadFilter()};
但后来我得到了各种属性值的另一个错误
//Uncaught TypeError: Cannot set property 'value' of undefined
对于使用内置浏览器方法和属性创建普通构造函数的正确方法,我真的很困惑。
我想将下面的代码抽象为类似于上面的代码
filter1 = context.createBiquadFilter();
filter1.type = 5;
filter1.gain.value = null;
filter1.Q.value = 1;
filter1.frequency.value = 80; // Changes
filter2 = context.createBiquadFilter();
filter2.type = 5;
filter2.gain.value = 0;
filter2.Q.value = 1;
filter2.frequency.value = 240; // Changes
filter3 = context.createBiquadFilter();
filter3.type = 5;
filter3.gain.value = 0;
filter3.Q.value = 1;
filter3.frequency.value = 750; // Changes
filter4 = context.createBiquadFilter();
filter4.type = 5;
filter4.gain.value = 0;
filter4.Q.value = 1;
filter4.frequency.value = 2200; // Changes
filter5 = context.createBiquadFilter();
filter5.type = 5;
filter5.gain.value = 0;
filter5.Q.value = 1;
filter5.frequency.value = 6000; // Changes