塞特犬不在某个地方被叫,在其他地方也可以正常工作

时间:2018-08-21 11:46:07

标签: javascript setter

我创建了一个js类:

class Drawer {

}

然后添加了一个带有单个参数的构造函数:

constructor (elemId) {
    // get element by passed id
    this.elem = document.getElementById(elemId);
    // make sure the element was found
    console.log(elem);

    // listen for clicks on the found element
    this.elem.addEventListener('click', this.handleDimClick);

    // listen for touch events (start, move, cancel, end)
    document.body.addEventListener('touchstart', this.handleTouchStart);
    ...
}

这会将正确的元素记录到控制台。

handleDimClick函数将属性设置为false

以下是相关属性:

get open() {
    return this.isOpen;
}

set open(value) {
    console.log('setting open to ' + value);
    this.isOpen = value;
    // code omitted
}

这是handleDimClick方法:

handleDimClick() {
    console.log('handleDimClick setting open to false');
    this.open = false;
    console.log('handleDimClick open should be false');
}

出于某种原因,这仅将两行记录到控制台:

handleDimClick setting open to false
handleDimClick open should be false

其中一种触摸处理功能还设置了open属性:

handleTouchEnd(e) {
    console.log('handleTouchEnd setting open to ' + (irrelevant));
    this.open = (irrelevant);
    console.log('handleTouchEnd open should be ' + (irrelevant));
}

并按预期记录到控制台:

handleTouchEnd setting open to false
setting open to false
handleTouchEnd open should be false

我绝对不知道为什么会这样。我尝试按Ctrl + Shift + R进行刷新而没有缓存,但这并没有帮助。我可以在开发人员控制台中看到文件正常。行为是一致的。

0 个答案:

没有答案