Javascript说我的类方法不是函数,尽管它显然是函数

Javascript说我的类方法不是函数,尽管它显然是函数,javascript,node.js,Javascript,Node.js,我正在尝试使用node实现一个遗传算法,我看了这里发布的另一个问题,它似乎不相关。当我尝试调用我的populant类的determinate\u fitness方法时,我得到以下错误: Creating new population Starting iteration 1 /home/aaron/Documents/workspace/playground/screeps_ga/ga.js:116 p.determine_fitness();

我正在尝试使用node实现一个遗传算法,我看了这里发布的另一个问题,它似乎不相关。当我尝试调用我的
populant
类的
determinate\u fitness
方法时,我得到以下错误:

Creating new population

Starting iteration 1
/home/aaron/Documents/workspace/playground/screeps_ga/ga.js:116
            p.determine_fitness();
              ^

TypeError: p.determine_fitness is not a function
    at population.fitness (/home/aaron/Documents/workspace/playground/screeps_ga/ga.js:116:15)
    at population.train (/home/aaron/Documents/workspace/playground/screeps_ga/ga.js:130:18)
我对javascript真的很陌生,所以我不知道除了遵循stacktrace之外还能做些什么,它没有给我任何答案

但是,当我看我的课程时:

类populant{
构造器(基因){
这个。基因=基因;
这个分数=-1;
}
突变{
if(this.genes.length==0)
this.genes+=random_char();
否则{
设r=getRndInteger(0,3);
设i=getRndInteger(0,this.genes.length);
开关(r){
案例0:
//删除字符
let before=this.genes.substring(0,i);
let after=this.genes.substring(i+1);
this.genes=之前+之后;
打破
案例1:
//替换字符
设c=random_char();
let before=this.genes.substring(0,i);
let after=this.genes.substring(i+1);
this.genes=before+c+after;
打破
案例2:
//添加字符
设c=random_char();
let before=this.genes.substring(0,i);
let after=this.genes.substring(i);
this.genes=before+c+after;
打破
}
}
}
配偶(其他,变异){
设i=0;
设temp='';
对于(i=0;i
正如您所看到的,它在这里定义得非常清楚,并且它与我的任何其他属性名称都没有冲突,所以我真的很困惑

类人口{
构造函数(计数、变异率、人口类型){
this.count=计数;
这个。突变率=突变率;
this.populant=populant\u类型;
this.pop=[];
}
初始化(父池){
this.pop=[];
if(parent_pool.length==0)
for(设i=0;i
我现在组织这些类的方式是,
populat
类和
population
类在它们自己的文件
ga.js
中,然后我将它们导入我的
test.js
文件中。由于默认情况下未定义
populant
determinate\u fitness()
方法,因此我创建了一个子类并重写了它,如下所示:

让ga=require('./ga');
类StringPop扩展了ga.populant{
构造器(基因){
超级(基因);
}
确定适合度(){
设和=0;
for(设i=0;i
然后我测试结构,如图所示:

pop=新的遗传群体(20,0.25,StringPop);
设迭代=1;
让父母=[]
while(true)
{
console.log('\n开始迭代'+iteration.toString());
父母=大众列车(1,5,父母);
log(“迭代后的填充”);
打印持久性有机污染物(pop);
}

其中,
print\u pops()
只需将每个群体的
基因
属性打印在自己的行上。

在尝试调用determine\u fitness之前,您可以通过记录来检查p的类型是否符合您的预期:

fitness() {
    for(let p in this.pop) {
        console.log(typeof p);
        p.determine_fitness();
    }    
}
另外,因为pop是一个列表,我想你可以把一个对象推到它上面,你可以使用。。。语法:

fitness() {
    for (p of this.pop) {
        p.determine_fitness();
    }
}
从:

鉴于for…in是为迭代对象属性而构建的,不建议与数组一起使用,以及诸如Array.prototype.forEach()和for…of exist之类的选项,那么for…in到底有什么用途呢?[我的重点]


如果您使用TypeScript的
tsc
编译此文件会发生什么?您发布的代码中哪里是
ga.js
,第116行?
ga.js
population
population
的组合好吧,我已经尝试让
tsc
工作,但我看不出答案,我用t查看