@tostershmoster

Как забиндить this для метода jquery плагина?

По отдельности методы работают, но если добавить строку methods['destroy'](); в метод init получаю ошибку Uncaught TypeError: this[0] is undefined

(function ($) {

  let methods: any = {

    init: function(userOptions: IUserSettings): JQuery {
      methods['destroy'](); // Uncaught TypeError: this[0] is undefined
      // code ...
    },

    destroy: function() {
      let slider = document.querySelector(`#${this[0].id}`); // error
      // code ...

      }
    }
  };

  $.fn.RangeSlider = function (method: IMethod): JQuery {


    if ( methods[method] ) {
      return methods[method].apply(this, Array.prototype.slice.call( arguments, 1));
    } else if (typeof method === 'object' || ! method) {
      return methods.init.apply(this, arguments);
    } else {
      $.error(`Method ${method} is not exist in jQuery.RangeSlider`);
    }
    return this;

  };
}(jQuery));
  • Вопрос задан
  • 52 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы