2017-02-25 67 views
1

所以我對這個p5js腳本有點麻煩。我得到一個TypeError,說「this.randomGenes不是一個函數」,但它看起來像一個我......我不明白錯誤來自哪裏。它的拼寫都是正確的,所有的分號都在那裏,所有的括號都是關閉的,所有的括號都是。這個錯誤根本不會在我身上突出。'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

你需要讓你的DNA功能的「實例」,以便this綁到一個對象實例,然後你可以從實例調用randomGenes。如果您只運行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不客氣。不要忘記對答案投票並標記爲「答案」。 –