У меня есть набор объектов jQuery, вдавленных в массив "разделов". Я добавляю прослушиватель событий.on, и я консолью, чтобы проверить, вызвана ли функция, но это не так.
sections.push($('#section' + p_sectionIndex));
sections[sections.length-1].on('show', this.showSection);
this.showSection = function() {
console.log("Section should show");
}
В другом месте кода я вызываю:
sections[showIndex].trigger('show');
Что я делаю не так?
Когда вы используете this.showSection
оно не определено, поскольку выражение функции еще не выполнено. Таким образом, обработчик не зарегистрирован
this.showSection = function () {
console.log("Section should show");
}
sections.push($('#section' + p_sectionIndex));
sections[sections.length - 1].on('show', this.showSection);
Демо: скрипка
Но если вы используете объявление функции, оно будет поднято
jQuery(function () {
function handler1() {
console.log('handler1')
}
console.log('handler1', handler1)
console.log('handler2', handler2)
$('button').on('click', handler1).on('click', handler2)
function handler2() {
console.log('handler2')
}
})
Демо: скрипка