2017-02-25 1 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 위해서는 당신의 DNA 기능의 "인스턴스"를 만들 필요가 다음 해당 인스턴스에서 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 여러분을 환영합니다. 답을 표기하고 "답"으로 표시하는 것을 잊지 마십시오. –