我只是试图从另一个类方法中的函数中访问一个类方法,所以:
class test
{
show()
{
setTimeout(function()
{
this.showInside()
},0)
}
showInside()
{
alert("WORKING")
}
}
var test2 = new test();
test2.show()
我显然做错了,很明显我不能在该函数中使用this.showInside(),但我不知道该怎么做...
有帮助吗?
答案 0 :(得分:0)
this
取决于上下文。在setTimeout
内部,this
没有引用实例。这是一个工作示例:
class test
{
constructor(){};
show(){
setTimeout((function(){ this.showInside() }).bind(this),0)
}
showInside(){
alert("WORKING");
}
}
var test2 = new test();
test2.show();