After a lot of reading I finally managed to make Babel work and thus transpile the scripts.
Before using das, let's call it 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 ) );
Then, slowly, I was trying to do the implementation with the 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 as soon as the Menu object was instantiated the Navigation constructor > would call the init method, initializing the Event.
It works, but not as I imagined.
In Location in fact, 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 inside of jQuery.on () I get an undefined o.
I figured maybe the scope of Navigation.init()
could be overwritten and debug only it and got the clicked node (an image).
And I wanted to know why, of course, but also how to fix this to integrate properly, if possible, but preferably without having to, for example, inject the scope this
into this
:
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()
}
}