2017-02-25 64 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不客气。不要忘记对答案投票并标记为“答案”。 –