2017-02-25 7 views
1

私はこのp5jsスクリプトで少し問題があります。私は "this.randomGenesは関数ではない"と言っているTypeErrorを得ていますが、それは私のように見えます...私はエラーがどこから来ているのか分かりません。すべての綴りが正しい、すべてのセミコロンはそこにあり、すべての括弧は閉じている、すべての括弧も。エラーは私には目立たない。'this.randomGenes'はどのように関数ではありませんか?

function DNA(genes) { 
    this.maxWeight = 25; 
    this.maxSpeed = 25; 

    if (genes) { 
     this.genes = genes; 
    } else { 
     this.genes = []; // weight, position, maxspeed, rgba 
     this.randomGenes(); 
    } 

    this.randomGenes = function() { 
     this.genes[0] = random(this.maxWeight); 
     this.genes[1] = [random(height), random(width)]; 
     this.genes[2] = random(this.maxSpeed); 
     this.genes[3] = [random(255), random(255), random(255), random(255)]; 
    } 
} 
+2

コードを順番に読んで:)あなたはそれを呼んでいる点で、それはまだ定義されていません。 – qqilihq

+0

'this'はデフォルトで' function'にスコープされます –

+0

あなたはどこでメソッドを呼び出していますか? –

答えて

1

あなたはthisは、そのオブジェクトのインスタンスにバインドすると、あなたはそのインスタンスからrandomGenesを呼び出すことができるためには、あなたのDNAの機能の「インスタンス」を作成する必要があります。 DNA関数を実行すると、thisが正しくバインドされず、関数が見つからないことがあります。

function DNA(genes) { 
 
    this.maxWeight = 25; 
 
    this.maxSpeed = 25; 
 

 
    if (genes) { 
 
     this.genes = genes; 
 
    } else { 
 
     this.genes = []; // weight, position, maxspeed, rgba 
 
     this.randomGenes(); 
 
    } 
 

 
    this.randomGenes = function() { 
 
     this.genes[0] = random(this.maxWeight); 
 
     this.genes[1] = [random(height), random(width)]; 
 
     this.genes[2] = random(this.maxSpeed); 
 
     this.genes[3] = [random(255), random(255), random(255), random(255)]; 
 
    } 
 
} 
 

 
// Make an instance of the DNA object so that `this` gets bound to it 
 
var DNA1 = new DNA("myGenes"); 
 

 
// Now, you can call the function via the instance 
 
// Here, this will cause an error about "random" not being defined, but 
 
// that actually proves that "randomGenes" was invoked. 
 
DNA1.randomGenes();

、@qqilihqコメントで述べたように任意の引数が渡されることなく、あなたのインスタンスが作成されている場合、それがされています前に、あなたが関数を呼び出すしようとしているので、あなたはエラーになりますメソッドとして割り当てられます。これを修正するには、コードを変更する必要がありますが、別の理由で同じ変更が行われる必要があります...

関数の新しいインスタンスを作成すると、この関数は "関数コンストラクタ"それを呼び出してオブジェクトインスタンスを構築します。オブジェクトのすべてのインスタンスは同じ動作(メソッド)を(通常は)使用するので、通常、これらのメソッドをすべてのインスタンスが継承するオブジェクトの基本的な「プロトタイプ」オブジェクトに追加します。この方法では、関数を一度保存​​すればすべてのインスタンスが継承します。関数をプロトタイプに移動することで、インスタンスが実際に作成されて解決される前にメモリに格納されているため、問題だけでなく効率も向上します。

だから、あなたのコードは本当にする必要があります:

function DNA(genes) { 
 
     // Instance properties get created using the "this" keyword 
 
     // inside the constructor function 
 
     this.maxWeight = 25; 
 
     this.maxSpeed = 25; 
 

 
     if (genes) { 
 
      this.genes = genes; 
 
     } else { 
 
      this.genes = []; // weight, position, maxspeed, rgba 
 
      this.randomGenes(); 
 
     } 
 
    } 
 
    
 
    // By adding the function to the prototype of DNA, all instances constructed 
 
    // via the DNA constructor function will inherit the method: 
 
    DNA.prototype.randomGenes = function() { 
 
      this.genes[0] = random(this.maxWeight); 
 
      this.genes[1] = [random(height), random(width)]; 
 
      this.genes[2] = random(this.maxSpeed); 
 
      this.genes[3] = [random(255), random(255), random(255), random(255)]; 
 
    } 
 

 
    // Make an instance of the DNA object so that `this` gets bound to it 
 
    var DNA1 = new DNA(); 
 

 
    // Now, you can call the function via the instance 
 
    // Here, this will cause an error about "random" not being defined, but 
 
    // that actually proves that "randomGenes" was invoked. 
 
    DNA1.randomGenes();

+0

私はプロトタイプX.Dをまだ使用していません。私は何度もそれらを見てきました。私はそれらについてもっと学ばなければならないでしょう – theDr34mer

+0

アップデート:私があなたが言及したプロトタイプのバージョンをやりました。お手伝いありがとう! – theDr34mer

+0

@ theDr34merよろしくお願いします。答えを投票して、 "その"答えとマークすることを忘れないでください。 –

関連する問題