If you define a function inside a class, the function definition is automatically modified to have
this as its first parameter.
If you define a function outside of a class, and then assign it to an object, and then call it like a method
object.function(params), the object will automatically pass itself as the first parameter of the function. Potentially ruining the parameter order if it matters.
When you do
varName := this.menuhandle, out of a static or instance method, you are getting menuhandle that is defined as the following order (for the purposes of the callback)
(this, ItemName, ItemPos, MyMenu).
If you then do,
this.menuhandle.bind(this), your boundfunc now has the proper order as
(ItemName, ItemPos, MyMenu), since
this is bound to the first parameter.
this.menuhandle.call(this, var_argv*)
If you are extracting a method, you need to bind
this, if the parameter order matters and you are going to use object in
this.
You either bind the method to its
this
Code: Select all
b.a.show
class b {
static __New(){
this.a:=menu()
this.a.add "a", this.menuhandle.bind(this) ; msg "a" *********************************************
this.a.add "b", menuhandle ;~ msg "b"
}
static menuhandle(item,*){
MsgBox item
}
}
menuhandle(item,*){
MsgBox item
}
Or do type introspection on the hidden
this and lose the ability to reference the object (class object in this case).
Code: Select all
b.a.show
class b {
static __New(){
this.a:=menu()
this.a.add "a", this.menuhandle ;~ msg "a"
this.a.add "b", menuhandle ;~ msg "b"
}
static menuhandle(item,*){
if ( type(this) = "string" ) ; ********************************************************
item := this
MsgBox item
}
}
menuhandle(item,*){
MsgBox item
}
You could also wrap the callback with
this.a.add "a", (item, *) => this.menuhandle(item) and close the
this in the scope.
Ultimately,
this is an extra parameter, if you are not referencing it inside the function, you can bind it to anything, even 0.