Why does one display 1 and the other display "b"? --Menu Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
cgx5871
Posts: 318
Joined: 26 Jul 2018, 14:02

Why does one display 1 and the other display "b"? --Menu

Post by cgx5871 » 26 Dec 2023, 23:24

Code: Select all

#Requires AutoHotkey v2
#SingleInstance

b.a.show
class b {
	static __New(){
		this.a:=menu()
		this.a.add "a", this.menuhandle		;~ msg 1
		this.a.add "b", menuhandle			;~  msg "b"
	}
	static menuhandle(item,*){
		MsgBox item
	}
}

menuhandle(item,*){
	MsgBox item
}
this.a.add "a", this.menuhandle ;~ msg 1
this.a.add "b", menuhandle ;~ msg "b"

User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Why does one display 1 and the other display "b"? --Menu

Post by boiler » 27 Dec 2023, 06:07

I don’t know why, but apparently in this case, the callback function does not include the ItemName parameter.

Code: Select all

#Requires AutoHotkey v2
#SingleInstance

b.a.show
class b {
	static __New(){
		this.a:=menu()
		this.a.add "a", this.menuhandle		;~ msg 1
		this.a.add "b", this.menuhandle		;~ msg 2
	}
	static menuhandle(ItemPos, MyMenu){
		MsgBox ItemPos ' : ' MyMenu.Handle
	}
}

coffee
Posts: 133
Joined: 01 Apr 2017, 07:55

Re: Why does one display 1 and the other display "b"? --Menu  Topic is solved

Post by coffee » 27 Dec 2023, 06:38

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.

niCode
Posts: 315
Joined: 17 Oct 2022, 22:09

Re: Why does one display 1 and the other display "b"? --Menu

Post by niCode » 27 Dec 2023, 07:23

Alternatively:

Code: Select all

this.a.add "a", ObjBindMethod(this, 'menuhandle')

cgx5871
Posts: 318
Joined: 26 Jul 2018, 14:02

Re: Why does one display 1 and the other display "b"? --Menu

Post by cgx5871 » 27 Dec 2023, 15:43

@coffee
@niCode
thank you both

Post Reply

Return to “Ask for Help (v2)”