Как обратиться к методу объекта из колбэка в JS?

151
18 сентября 2018, 10:40

Вот у меня есть код:

class BaseInput{
    constructor(value){
        this.makeButtonVisible = null;
        this.makeButtonVisible = function(){
            alert(1234);
        };
        this.makeButtonInvisible = null;
        this._input = document.createElement('input');
        this._input.type = 'text';
        this._input.value = value;
        this._input.dataset.value = value;
        this._input.oninput = function(){
            if (this.value != this.dataset.value)
               BaseInput.makeButtonVisible();
            else
                alert(321);
        }
        document.querySelector('.container').appendChild(this._input);
    }
}

в this._input.oninput я добавляю функцию, которая должна обратиться к методам makeButtonVisible или makeButtonInvisible этого объекта

Answer 1
    this._input.dataset.value = value;
    this._input.logic = this;
    this._input.oninput = function(e) {
        if (this.value != this.dataset.value)
            this.logic.makeButtonVisible();
        else
            alert(321);
    };

или

    this._input.dataset.value = value;
    this._input.oninput = (e) => {
        if (this._input.value != this._input.dataset.value)
            this.makeButtonVisible();
        else
            alert(321);
    };

или

    this._input.dataset.value = value;
    this._input.oninput = (function(e) {
        if (this._input.value != this._input.dataset.value)
            this.makeButtonVisible();
        else
            alert(321);
    }).bind(this);
READ ALSO
Разделить строку с помощью js

Разделить строку с помощью js

Есть такая строка: Пн: 10:00-19:00 Вт: 10:00-19:00 Ср: 10:00-19:00 Чт: 10:00-19:00 Пт: 10:00-19:00 Сб: 09:00-17:00 Вс: Вихідний Всё идёт в рядМне нужно как-то вывести строку, чтобы...

244
Как передать параметры в View React Native

Как передать параметры в View React Native

Не давно начал изучение react nativeИ столкнулся с проблемой

168
Ошибка Uncaught SyntaxError: Unexpected end of JSON input в JavaScript

Ошибка Uncaught SyntaxError: Unexpected end of JSON input в JavaScript

Не могу распарсить json - выдает ошибку

147
MySQL вопрос по хранению данных

MySQL вопрос по хранению данных

База данных содержит информацию про фильм:

158