Ошибка при попытке создания статического метода класса

Есть клас:

class Student {
    constructor(fullName, direction) {
        this._fullName = fullName;
        this._direction = direction;
    }

    showFullName() {
        let result = this._fullName;
        return result;
    }

    nameIncludes(data) {
        if (this.showFullName().includes(data)) return true;
        return false;
    }

    static studentBuilder(fullName, direction){
        let result = {
            _fullName: fullName,
            _direction: direction 
        };
        return result;
    }
}

const stud1 = new Student("Ivan Petrenko", "web");
const stud2 = new Student("Sergiy Koval", "python");
const stud3 = Student.studentBuilder("Ihor Kohut", "qc");

При вызове метода nameIncludes() для экземпляров stud1 и stud2 все отработывает отлично, но при попытке вызова метода для экземпляра stud3 все идёт не по плану. Я полагаю, что это из-за того, что это статический метод, но почему это так работает я не знаю, так же, как не знаю, как это исправить.

Error


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

Автор решения: Aziz Umarov

Сатический метод Student.studentBuilder

const stud3 = Student.studentBuilder("Ihor Kohut", "qc");

не возращяет нового Student. Только объект

    {
        _fullName: fullName,
        _direction: direction 
    };

Так что не понятны ваши ожидания по поводу nameIncludes()

Как правильно заметил @Алексей Шиманский Вам нужно

class Student {
    constructor(fullName, direction) {
        this._fullName = fullName;
        this._direction = direction;
    }

    showFullName() {
        let result = this._fullName;
        return result;
    }

    nameIncludes(data) {
        if (this.showFullName().includes(data)) return true;
        return false;
    }

    static studentBuilder(fullName, direction){
        return new Student(fullName,direction);
    }
}
→ Ссылка