struggling to follow the documentation, accessing a objects properties inside its method Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ralf_Reddings200244
Posts: 110
Joined: 11 Mar 2023, 14:16

struggling to follow the documentation, accessing a objects properties inside its method

Post by Ralf_Reddings200244 » 08 Apr 2024, 16:47

I am trying to understand, the Protoypes section of the objects page, here is what I have:

Code: Select all

obj		        := {}
obj.prop         := 10
obj.meth         := func("myMeth")
obj.meth(10)
print(obj.prop)
 
myMeth(inp){
	print("inside myMeth()")			;To confirm if method is called
	print(this.prop)						;should print "10"
	this.prop := this.prop + inp		;should add 10 to 10 
	print(this.prop)						;should print "20"
}
It seems myMeth() does not have access to the obj.prop value, I have ruled out that the method does get called when obj.meth(10) is invoked.

I want to access the various properties belonging to obj inside myMeth()

I understand the Classes section, explains how to properly define a class but I really want to know how this works too.


I know, that V1 is as dead as latin, but until the library porting for V2 catches up, I will be on V1.

Any help would be greatly appreciated!

ShatterCoder
Posts: 88
Joined: 06 Oct 2016, 15:57

Re: struggling to follow the documentation, accessing a objects properties inside its method  Topic is solved

Post by ShatterCoder » 09 Apr 2024, 12:39

you are pretty close but what you missed from the docs (and it's not super clear) is that when calling a method from a prototype the first argument for the function def should be "this" as in:

Code: Select all

obj		        := {}
obj.prop         := 10
obj.meth         := func("myMeth")
obj.meth(10)
print("obj.prop outside the method " obj.prop)
return

myMeth(this, inp){
	print("inside myMeth()")			;To confirm if method is called
	print("this.prop still inside the method/func " this.prop)						;should print "10"
	this.prop := this.prop + inp		;should add 10 to 10
	print("this.prop still inside the method/func "this.prop)						;should print "20"
}
return

ShatterCoder
Posts: 88
Joined: 06 Oct 2016, 15:57

Re: struggling to follow the documentation, accessing a objects properties inside its method

Post by ShatterCoder » 09 Apr 2024, 13:13

To be a bit more clear, when you assign a method to an object, ahk automatically passes the object to the function as it's first argument, you could name it anything you like inside the method/function, it does not specifically have to be "this". Though for clarity and ease of reading it is recommended.
Something to keep in mind, is that as far as ahk knows, obj.method() is just a reference to the function method() so anything that applies to functions in general, applies to method() (including scope rules).

Ralf_Reddings200244
Posts: 110
Joined: 11 Mar 2023, 14:16

Re: struggling to follow the documentation, accessing a objects properties inside its method

Post by Ralf_Reddings200244 » 11 Apr 2024, 11:03

@ShatterCoder
I finally get it now, thank you for this and for the heads up about the scoping too. Cheers!

Post Reply

Return to “Ask for Help (v1)”