【发布时间】:2016-11-11 07:19:03
【问题描述】:
我基本上想在静态“bar”方法中访问“foo2”方法,但到目前为止我只能访问“foo1”和“foo3”方法。谁能教我如何访问非静态 foo2 方法。
let foo1 = () => {
alert('foo1’);
};
class ABC extends Component {
static bar() {
foo1(); //can work
foo2; //not work with error "Can't find variable foo2"
foo2(); //not work with error "Can't find variable foo2"
this.foo2(); //not work with error "this.foo2 is not a function"
this.foo2; //no error but no alert, basically nothing happen
ABC.foo3() //can work
}
foo2 = () => {
alert('foo2');
};
static foo3 = () => {
alert('foo3');
};
}
module.exports = ABC;
【问题讨论】:
-
你是用打字稿还是只用javascript?
-
我认为你不能使用箭头函数作为类方法。
-
这是不可能的;用一个真实的例子解释你为什么需要它,将使我们能够提供替代方案。
-
@KonstantinVitkovsky 其实我想这样做的原因是因为我想覆盖 react native 路由器通量右键功能,这样我就可以为 webview 调用 reload stackoverflow.com/questions/40524030/…
-
@Newbie009 好的,但是这个问题适用于任何 js/ts 应用程序 :-) 顺便说一句,我的回答对你有帮助吗,或者还有什么要解决的吗?)
标签: javascript typescript ecmascript-6