Access parent object variable/property from inner/nested object Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
andrict05
Posts: 1
Joined: 27 May 2021, 11:07

Access parent object variable/property from inner/nested object

Post by andrict05 » 01 Oct 2022, 03:32

Hello,

I seriously cannot figure this out and I know it's probably something stupidly easy.

Code: Select all

Application := {
   Name: "User Interface",
   Version: "0.0.1",
   Interface: {
      Width: 600,
      Height: 400,
      Title: super.Name
   }
}

MsgBox(Application.Interface.Title . "")
I'm trying to use Application.Name as value of Application.Interface.Title, but I don't know how to refrence parent object within nested object.

Thanks.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Access parent object variable/property from inner/nested object

Post by swagfag » 01 Oct 2022, 05:30

theres no language support for this

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Access parent object variable/property from inner/nested object  Topic is solved

Post by lexikos » 01 Oct 2022, 05:31

I have moved the topic to the v2 Help forum, as your code is evidently for v2.

There is no "parent-child" or "outer-inner" relationship. It is just one object containing a reference to a second object, created by an expression.

Neither of the objects even exist at the time super.Name in your example would be evaluated. You need to create both objects first, then separately use Application.Interface.Title := Application.Name to copy the value.

Of course, super.Name is only valid inside a class definition, and it refers to the super-class implementation of the Name property, on the same object (this). It has nothing to do with any parent-child relationship.

MsgBox(WinGetTitle("A")) is a function call nested inside a function call, but there is no relationship between the two calls aside from being evaluated in sequence, and the inner call has no way to "reference" the outer call. The expression "compiles" to a sequence of instructions like this:
  • push MsgBox (onto a stack of values)
  • push WinGetTitle
  • push "A"
  • call with 1 parameter (pops value "A" and function WinGetTitle; calls WinGetTitle; pushes the title string)
  • call with 1 parameter (pops title string and function MsgBox; calls MsgBox; pushes the result)
Similarly, the expression {A: B, C: {D: E}} just produces a sequence of instructions like this:
  • push "A"
  • push B
  • push "C"
  • push "D"
  • push E
  • call internal object constructor with 2 parameters
  • call internal object constructor with 4 parameters
In AutoHotkey v1.1, {A: B} is literally equivalent to Object("A", B). It's just a function call. In v2, the function being called is internal and can't be referenced directly or redefined.

Post Reply

Return to “Ask for Help (v2)”