admin管理员组文章数量:1326327
As I understand things, this
is not available inside the constructor before super( )
is called.
Still, when referring to instance methods, we need to prefix the method with this
. So how is it possible to pass a instance method to super( )
?
e.g. In the Phaser framework, there is a Button
class. The constructor takes a callback for the click-event:
Constructor
new Button(game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame)
callback - The function to call when this Button is pressed.
callbackContext - The context in which the callback will be called (usually 'this').
I want my own button class, which I define like this:
class MyButton extends Phaser.Button {
constructor(game) {
super(game, game.world.centerX, game.world.centerY, 'buttonImage');
}
clickHandler(button, pointer) {
//handle the clicking
}
}
So how will I pass the clickHandler
to super
?
this.clickHandler
gives the error [Build Error] 'this' is not allowed before super() while parsing file: ....
and passing just clickHandler
gives me a runtime error of Uncaught ReferenceError: clickHandler is not defined
.
Any suggestions?
As I understand things, this
is not available inside the constructor before super( )
is called.
Still, when referring to instance methods, we need to prefix the method with this
. So how is it possible to pass a instance method to super( )
?
e.g. In the Phaser framework, there is a Button
class. The constructor takes a callback for the click-event:
Constructor
new Button(game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame)
callback - The function to call when this Button is pressed.
callbackContext - The context in which the callback will be called (usually 'this').
I want my own button class, which I define like this:
class MyButton extends Phaser.Button {
constructor(game) {
super(game, game.world.centerX, game.world.centerY, 'buttonImage');
}
clickHandler(button, pointer) {
//handle the clicking
}
}
So how will I pass the clickHandler
to super
?
this.clickHandler
gives the error [Build Error] 'this' is not allowed before super() while parsing file: ....
and passing just clickHandler
gives me a runtime error of Uncaught ReferenceError: clickHandler is not defined
.
Any suggestions?
Share Improve this question edited Sep 11, 2015 at 10:35 sdgluck 27.3k12 gold badges81 silver badges95 bronze badges asked Sep 11, 2015 at 7:39 VegarVegar 12.9k16 gold badges88 silver badges153 bronze badges1 Answer
Reset to default 10This is a good use-case for ES6 arrow functions, which are bound lexically to this
.
Here is a generic example of logging an instance-derived value by proxying the call to the instance method with an arrow function:
(Try it out on an ES6 REPL, or see it pile in babel.)
class A {
constructor(method) {
if(method) {
method()
return
}
this.callback()
}
message() {
return "a"
}
callback() {
console.log(this.message())
}
}
class B extends A {
constructor() {
super(() => this.callback())
}
message() {
return "b"
}
callback() {
console.log(this.message())
}
}
As you can see, doing this allows us to avoid immediately referencing the thisArg
of our new instance before our call to super
. In your given example this is implemented like this:
class MyButton extends Phaser.Button {
constructor(game) {
super(
game,
game.world.centerX,
game.world.centerY,
'buttonImage',
() => this.clickHandler()
);
}
clickHandler(button, pointer) {
//handle the clicking
}
}
本文标签: javascriptPassing an instance method to super with ES6 classesStack Overflow
版权声明:本文标题:javascript - Passing an instance method to super with ES6 classes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742203104a2432322.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论