Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击事件时在类中调用Javascript函数_Javascript_Es6 Class - Fatal编程技术网

单击事件时在类中调用Javascript函数

单击事件时在类中调用Javascript函数,javascript,es6-class,Javascript,Es6 Class,我正在尝试使用类用普通javascript编写一些代码。我可以在React和JQuery中轻松做到这一点,但我确实很难理解在普通javascript类中遇到的问题 这是我的密码: 班级游戏{ 建造师{ this.balls=[] 对于let i=1;i n!==球 回击球 } 牵引杆{ const ball=this.getBall console.logball绘制:+ball } } 让g=新游戏 g、 牵引杆 //const-draullbutton=document.getElemen

我正在尝试使用类用普通javascript编写一些代码。我可以在React和JQuery中轻松做到这一点,但我确实很难理解在普通javascript类中遇到的问题

这是我的密码:

班级游戏{ 建造师{ this.balls=[] 对于let i=1;i n!==球 回击球 } 牵引杆{ const ball=this.getBall console.logball绘制:+ball } } 让g=新游戏 g、 牵引杆 //const-draullbutton=document.getElementByIddraw\u-ball //drablllbutton.onclick=g.drablell您正在将drablell作为常规函数调用,因此它缺少此上下文

试试这个:

const drawBallButton = document.getElementById("draw_ball");
drawBallButton.onclick = () => g.drawBall();
您正在将trablell作为常规函数调用,因此它缺少此上下文

试试这个:

const drawBallButton = document.getElementById("draw_ball");
drawBallButton.onclick = () => g.drawBall();

在javascript中添加更多信息:我不知道为什么这个问题会被编辑,因为假设之前回答的问题没有帮助,但是@Guerric-p答案是我所需要的,而且正是我所需要的。要用javascript补充更多信息:我不知道为什么这个问题会被编辑,因为之前回答的问题没有帮助,但是@Guerric-p答案是我所需要的。