Как я могу получить доступ к элементу this
после привязки this
класс?ES6 - Как получить доступ к элементу `this` после привязки класса` this`?
Например, без привязки this
:
$(".button-open").click(function(event) {
console.log(this); // <a href="#" class="button-open">Open</a>
this.openMe();
});
с обязательным this
:
$(".button-open").click(function(event) {
console.log(this); // Polygon {windowHeight: 965, scrollNum: 0}
this.openMe();
}.bind(this));
Как я могу получить и доступ <a href="#" class="button-open">Open</a>
снова после связывания this
?
Полный код:
class Polygon {
constructor() {
this.windowHeight = $(window).height();
this.scrollNum = 0;
}
// Simple class instance methods using short-hand method
// declaration
init() {
var clickMe = this.clickMe.bind(this);
return clickMe();
}
clickMe() {
$(".button-open").click(function(event) {
console.log(this);
this.openMe();
}.bind(this));
$(".button-close").click(function(event) {
this.closeMe();
}.bind(this));
}
openMe() {
console.log(this.scrollNum); // 0
this.scrollNum = 200;
console.log(this.scrollNum); // 200
return false;
}
closeMe() {
console.log(this.scrollNum); // 200
return false;
}
}
export { Polygon as default}
Любые идеи?
EDIT:
Та же проблема с JQuery animate
:
$(".element").animate({}, 'fast', 'swing', function(event) {
console.log(this); // the element
}.bind(this));
После связывания:
$(".element").animate({}, 'fast', 'swing', function(event) {
console.log(this); // undefined
}.bind(this));
Любой глобальный или пуленепробиваемые способ получения в element
снова?
косяк мы получаем доступ через event.tar получить? – Geeky
С помощью селектора jQuery снова? – Li357
@ Geeky да для клика, но не для анимации, см. Мое редактирование выше. – laukok