0
After much reading I finally managed to do the Babel work and thus transcompile the scripts.
Before using das, let’s call ES2015 classes, after reading about best practices I had something like this:
( function( Navigation, window, document, $, undefined ) {
Navigation.menu = function() {
( '#menu' ).on( 'click wheel', function( e ) {
// Do something with DOM
});
}
Navigation.menu();
}( window.Navigation = window.Navigation || {}, window, document, window.jQuery ) );
So, little by little, I was trying to do the implementation with ES2015:
class Navigation {
constructor( data ) {
this.data = data;
this.init();
}
}
class Menu extends Navigation {
init() {
$( '#menu' ).on( 'click wheel', function( e ) {
// Do something with DOM
});
}
}
I created a base class with what is common to each type of navigation (menu, sidebar...) thinking that so Object Menu the manufacturer of Navigation would call the method init, Initiating the Event.
Works, but not as I imagined.
Within Navigation.init()
(Location actually, but only to contextualize an abstract method), if I try to access the properties of Navigation:
console.log( this.data );
It works perfectly as expected but within jQuery.on() I get one Undefined o. The
I imagined that, perhaps, the scope of this
could be overwritten and I debug only it and got the node clicked (an image).
And I wanted to know why, of course, but also how to fix it to integrate properly, if possible, but preferably without having to, for example, inject the scope this
in Navigation.init()
:
class Navigation {
constructor( data ) {
this.data = data;
this.init( this );
}
}
class Menu extends navigation {
init( _this ) {
// _this points to Navigation, inside or inside jQuery.on()
}
}
And start using
$( e.target )
instead of$( this )
is safe, no compatibility problems?– user5613506
@user5613506 I would say that in most cases yes, but the documentation mentions that there are cases where the
this
and thee.target
may not be the same element in case the event spread of some child element, but looking at the documentation it seems possible to use also thee.currentTarget
, I will include some more information in the reply– Leandro Godoy Rosa