当用户通过以下方式单击元素时,我正在尝试访问类数组:
window.addEventListener( 'mousedown', onMouseDown, false );
onMouseDown函数调用:
storage[0].object.callback();
class StorageUnit{
constructor(){
this.x = 20;
this.someText = "Hey"
this.goods = ["Test"];
//called from mouse click event
this.callback = this.showInfo;
}
showInfo(){
console.log("This does work");
console.log(this.x); // undefined
console.log(this.someText); // undefined
console.log(this.goods[0]); // returns nothing
}
}
当我在浏览器的控制台中打印此对象的变量时,它将返回字符串,因此数组不为空。当我尝试在控制台上打印一些纯文本并且不访问该类的任何变量时,该方法确实起作用。