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

时间:2019-05-02 10:46:24

标签: javascript syntax-error var p5.js

当我遇到未捕获的语法错误时,我正在尝试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();
    }
}

}

2 个答案:

答案 0 :(得分:1)

您不需要使用关键字var。 js在此行for (int i中的ALos无效。 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();
    }
  }
}

答案 1 :(得分:0)

也许,您正在使用构造函数创建类?可能是这样的:

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())
        }
    }
}