javascript - Babel transform this by _this -
i have bootstrap modification tooltip. , process js webpack/babel
a simplification of code be:
$('[data-toggle="tooltip"]').tooltip({ title: () => { return $(this).children('.tooltip-html-content').html(); } });
this should element, bootstrap call function with:
gettitle: function () { var title , $e = this.$element , o = this.options title = $e.attr('data-original-title') || (typeof o.title == 'function' ? o.title.call($e[0]) : o.title) return title }
the important line is:
o.title.call($e[0])
where $e[0] tooltip dom element.
well, when process babel result change this
_this
before assign _this value consider real this.
the question: can avoid conversion in babel specific function?
=====
final solution based in response:
gettitle: function gettitle() {
you've used arrow function, definition closes on this
it's created.
if don't want behavior, don't use arrow function:
$('[data-toggle="tooltip"]').tooltip({ title: function() { // ----^^^^^^^^^^ return $(this).children('.tooltip-html-content').html(); } });
re edit:
i want use arrow function. prefer babel exclusion eslint exclusion.
you can't, not gettitle
, since way gives access element setting this
. arrow functions by nature have fixed this
(by not having 1 @ all; close on 1 in execution context created, fixed). cannot use arrow function function want this
determined caller, such in case.
your choices either modify bootstrap, or use normal function. latter seems more reasonable thing do.
Comments
Post a Comment