1

TypeError: this.initializeSieve is not a function at PrimeGenerator.generatePrimeNumbers. I use constructor mode and prototype mode to create a class and I just call other functions declared in PrimeGenerator.prototype and it report error, did I call them wrong?

function PrimeGenerator() {
  this.s = 0;
  this.f = [];
  this.primes = [];
}

PrimeGenerator.prototype = {
  constructor: PrimeGenerator,
  generatePrimeNumbers: maxValue => {
    if (maxValue < 2) {
      return []; // 对不合理值返回空数组
    } else {
      this.initializeSieve(maxValue); // TypeError happens here!
      // this.sieve();
      // this.loadPrimes();
      return this.primes;
    }
  },
  initializeSieve: maxValue => {
    this.s = maxValue + 1;
    this.f = [];
    let i;

    // 将数组初始化为true
    for (i = 0; i < this.s; i++) {
      this.f[i] = true;
    }

    // 去掉已知的非素数
    this.f[0] = this.f[1] = false;
  }
};
const primeGenerator = new PrimeGenerator();
const centArr = primeGenerator.generatePrimeNumbers(100);
console.log(centArr);

0 Answers0