Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为一组Web Audio API节点创建简单构造函数_Javascript_Constructor_Web Audio Api - Fatal编程技术网

Javascript 为一组Web Audio API节点创建简单构造函数

Javascript 为一组Web Audio API节点创建简单构造函数,javascript,constructor,web-audio-api,Javascript,Constructor,Web Audio Api,我有一批Web音频API节点,看起来像下面的代码。我想把它抽象成一个简单的构造函数,但我遇到了麻烦。我不确定我做错了什么。最终结果应该是 function filterTemplate(name,freqVal){ this.name = context.createBiquadFilter(); this.name.type = 5; this.name.gain.value = null; this.name.Q.value = 1;

我有一批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

您的问题在于上下文变量的范围

var filter = new filterTemplate("theName",200);  //Uncaught TypeError: Cannot call method 'createBiquadFilter' of undefined 
。。。意味着上下文变量在您试图访问它的位置(在filterTemplate构造函数中)不可用。当你这么做的时候

this.name = function(){return context.createBiquadFilter()};
。。。您将函数分配给this.name,在函数运行之前,它不会尝试访问上下文,因此错误被删除。相反,在this.name中没有筛选器,而是函数,并且函数没有增益属性,因此在尝试设置this.name.gain.value时会出现错误


您应该查找定义上下文的位置,并确保可以从filterTemplate中访问该变量。

构建器模式非常适合这种情况。特别是当您可以设置许多属性时

您可以像这样创建一个简单的
FilterTemplate

function FilterTemplate(builder) {
    this.context = builder._context;
    this.context.type = builder._type;    
    this.context.gain.value = builder._gainValue;    
    this.context.Q.value = builder._qValue;                  
    this.context.frequency.value = builder._frequencyValue;
}
它将生成器对象作为构造函数参数。这是
生成器

FilterTemplate.Builder = function () {
    this._context = context.createBiquadFilter();
    this._type = 5;    
    this._gainValue = null;    
    this._qValue = 1;                  
    this._frequencyValue = 80;

    this.context = function (val) { 
        this._context = val; return this; 
    };

    this.type = function (val) { 
        this._type = val; return this; 
    };

    this.gainValue = function (val) { 
        this._gainValue = val; return this; 
    };

    this.qValue = function (val) { 
        this._qValue = val; return this; 
    };

    this.frequencyValue = function (val) { 
        this._frequencyValue = val; return this; 
    };
};
您可以根据需要进一步扩展此示例。 现在,您可以轻松地创建
FilterTemplates

var filter1 = new FilterTemplate(
    (new FilterTemplate.Builder()).frequencyValue(80)
);

var filter2 = new FilterTemplate(
    (new FilterTemplate.Builder()).frequencyValue(80).qValue(2)
);

那么您的问题是什么呢?var filter=newfiltertemplate(名称,200);//未捕获引用错误:未定义名称
未定义名称
。很抱歉,它应该是字符串,但还有其他问题(在上面编辑)。对于使用内置浏览器方法和属性创建普通构造函数的正确方法,我真的很困惑;函数内部似乎消除了所有错误。但这样做是“膨胀”吗?我想要一个AudioContext();整个程序都可以访问。我觉得这是更好的礼节。谢谢btwYes,这绝对是整个计划的一个背景。使其可访问的最简单方法是在与filterTemplate相同的级别上定义它。如果看不到完整的代码,很难给出比这更好的建议。:)我想我的问题是我定义了AudioContext();在函数下面而不是上面…哈哈。我总是过度分析每件事,回想起来很有趣。我可以删除这篇文章,但我想我会出于娱乐目的而离开它,除非有人提示我这样做。谢谢。这看起来很整洁。我用叉子叉的。