0

以下のコードのような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
4

2 に答える 2

1

ビルダーパターンは、この状況に非常に適しています。特に、多くのプロパティを設定できる場合。

http://jsfiddle.net/yw8Fm/

このような単純な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;
}

コンストラクター引数としてビルダーオブジェクトを取ります。これがBuilderです。

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)
);
于 2013-03-09T09:38:05.877 に答える
0

問題は、コンテキスト変数のスコープにあります。

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内からその変数にアクセスできることを確認することです。

于 2013-03-09T09:19:40.650 に答える