My question is pertaining to the best practice for accessing a child object's parent. So let's say a class instantiates another class, that class instance is now referenced with an object. From that child object, what is the best way to reference back to the parent object? Currently I know of a couple ways that I use often, but I'm not sure if A) there is a better way to do it or B) which of them is the better practice
The first method is to use getDefinitionByName, which would not instantiate that class, but allow access to anything inside of it that was publicly declared.
_class:Class = getDefinitionByName("com.site.Class") as Class;
And then reference that variable based on its parent to child hierarchy.
Example, if the child is attempting to reference a class that's two levels up from itself:
_class(parent.parent).function();
This seems to work fine, but you are required to know the level at which the child is at compared to the level of the parent you are attempting to access.
I can also get the following statement to trace out [object ClassName] into Flash's output.
trace(Class);
I'm not 100% on the implementation of that line, I haven't persued it as a way to reference an object outside of the current object I'm in.
Another method I've seen used is to simply pass a reference to this into the class object you are creating and just catch it with a constructor argument
var class:Class = new Class(this);
and then in the Class file
public function Class(objectRef:Object) {
_parentRef = objectRef;
}
That reference also requires you to step back up using the child to parent hierarchy though.
I could also import that class, and then use the direct filepath to reference a method inside of that class, regardless of its the parent or not.
import com.site.Class;
com.site.Class.method();
Of course there the parent to child relationship is irrelevant because I'm accessing the method or property directly through the imported class.
I just feel like I'm missing something really obvious here. I'm basically looking for confirmation if these are the correct ways to reference the parent, and if so which is the most ideal, or am I over-looking something else?
Thanks in advance for taking the time to help a newbie out!