autohotkey

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


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

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!


Solution

  • It seems you're expecting this to be set to to the outer object without actually specifying it in in the parameter list as shown in the documentation you linked.

    The fix is easy, just add in the first parameter:

    obj              := {}
    obj.prop         := 10
    obj.meth         := func("myMeth")
    obj.meth(10)
    print(obj.prop)
    
    myMeth(obj, inp)
    {
        print("inside myMeth()")          ;To confirm if method is called
        print(obj.prop)                   ;should print "10"
        obj.prop := obj.prop + inp        ;should add 10 to 10
        print(obj.prop)                   ;should print "20"
    }
    
    print(something)
    {
        MsgBox, % something
    }