Javascript-对象字段未定义
我会尽量把我计划的重要部分发布出来。如果需要更多的代码,请告诉我 main.htmlJavascript-对象字段未定义,javascript,class,ecmascript-6,Javascript,Class,Ecmascript 6,我会尽量把我计划的重要部分发布出来。如果需要更多的代码,请告诉我 main.html <html> <head> <title>Mathematician</title> <meta charset=UTF8 /> </head> <body> <span id=player_information> </
<html>
<head>
<title>Mathematician</title>
<meta charset=UTF8 />
</head>
<body>
<span id=player_information>
</span>
<hr />
<span id=exercises>
</span>
</body>
<script src="script.js"></script>
</html>
我的目标是什么?我试图找出它,但这个错误消息对我来说没有意义。您必须将
this
对象绑定到函数,才能将this
用作类对象
因为函数中的this
对象基本上是指调用它的人的this
对象
假设您有一个HTML
代码:
<button onclick=Display()>Display</button>
或者,您可以将函数声明为箭头函数:
display=()=>{
//my function code goes here.
}
在构造器中,您希望将
此
绑定到显示并检查函数
尝试:
“right”的拼写也没有“w”;) 在构造函数中将
this
作为this.functionname=this.functionname.bind(this)
绑定到函数,或者使用箭头函数作为functionname=()=>{}
。不幸的是,这两种方法都不起作用。如果在构造函数中添加“this.display=this.display.bind(this);”,则会收到相同的错误消息。如果我尝试第二种变体,浏览器会显示“错误的方法定义”。
<button onclick=Display()>Display</button>
constructor()
{
this.display = this.display.bind(this);
}
display=()=>{
//my function code goes here.
}
constructor(player, number_of_exercises)
{
...code...
this.check_exercises = this.check_exercises.bind(this)
this.display = this.display.bind(this)
}