There is a 'this' problem. I meant an instance by this
, but it actually means <button>
element. How can I add a value to the instance's array?
class MyClass {
constructor() {
this.arr = [];
}
addValue(event) {
console.log(this); // 'this' is <button>add</button>
this.arr.push('added'); // Error!
console.log(this.arr);
}
}
const c = new MyClass();
const btn = document.querySelector('button');
btn.addEventListener('click', c.addValue);
<button>add</button>