Методы JavaScript

В коде ниже массивы должны возвращать значения из массива values, но из-за ошибки в терминал выдает, что ошибка указывает на то, что один из объектов, для которых вызывается ".filter", не определен. Как исправить?

class Numbers {
        #values;
        #threshold;

        constructor(threshold, values) {
            this.#values = values;
            this.#threshold = threshold;
        }

        // Возвращает все значения из массива values, которые больше, чем значение переменной threshold
        getGreaterThanThreshold() {
            return this.values.filter(function (v) {
                return v > this.threshold;
            })
        };

        // возвращает все значения из массива values, которые меньше, чем значение переменной threshold 
        getLessThanThreshold() {
            return this.values.filter(function (v) {
                return v < this.threshold;
            })
        }
    };

    let numbers = new Numbers(50, [10, 20, 23, 11, 5, 6, 90, 33, 45, 67]);
    let gt = numbers.getGreaterThanThreshold();
    let lt = numbers.getLessThanThreshold();

    console.log(gt);
    console.log(lt);

Ответы (1 шт):

Автор решения: Rudi

Переменные this.#threshold и this.threshold разные. Внутри filter переменная this.threshold не видна..

class Numbers {
        #values;
        #threshold;

        constructor(threshold, values) {
            this.values = values;
            this.threshold = threshold;
        }

        // Возвращает все значения из массива values, которые больше, чем значение переменной threshold
        getGreaterThanThreshold() {
          let thr = this.threshold;
            return this.values.filter(function (v) {
                return v > thr
            })
        };

        // возвращает все значения из массива values, которые меньше, чем значение переменной threshold 
        getLessThanThreshold() {
          let thr = this.threshold;
            return this.values.filter(function (v) {
                return v < thr;
            })
        }
    };

    let numbers = new Numbers(50, [10, 20, 23, 11, 5, 6, 90, 33, 45, 67]);
    
    let gt = numbers.getGreaterThanThreshold();
    let lt = numbers.getLessThanThreshold();

    console.log(gt);
    console.log(lt);

→ Ссылка