JS 和 Html 中未捕获的语法错误意外标识符

Uncaught syntax error unexpected identifier in JS and Html

当我遇到未捕获的语法错误时,我正在尝试 p5.js。我已经多次扫描了所有代码,但我终生无法获得它。提前感谢您的努力!

class Population {
var mutRate; // ERROR LINE
var population;        
function Population(pop, m) {
    mutRate = m;
    population = new DNA[pop];
    for (int i = 0; i < population.length; i++) {
      population[i] = new DNA();
    }
}

}

您不需要使用关键字 var。 ALos在js中这一行for (int i是无效的。 js

中没有int

class Population {
  mutRate; // ERROR LINE
  population;
  population(pop, m) {
    mutRate = m;
    population = new DNA[pop];
    for (let i = 0; i < population.length; i++) {
      population[i] = new DNA();
    }
  }
}

也许,您正在使用构造函数制作 class?它可以是这样的:

class Population {
    // Javascript classes do not support any pre-declared fields
    // Maybe, as population function you meant constructor?
    constructor(pop, m){
        this.mutRate = m; // Use "this" to acess to object properties
        this.population = [] // Javascript not supports types
        // So we will create an empty array
        // And fill it with objects
        for(let i = 0; i < p.length; i++){
            this.population.push(new DNA())
        }
    }
}