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?
It’s generally good to have the class as it’s own instance and reduce tight coupling to something else (as in this case, it’s parent). If you do something like parent.doSomething() it’s not possible to use that class in container that doesn’t have the doSometing() method. I think it’s definitely better to pass in whatever the class may need and then inside the class it doesn’t have to do any parent.parent etc anymore.
With this if you in the future want to change the structure, it’s very easy to just pass in a new reference; the implementation of the child class doesn’t have to change at all.
The third alternative you have here is also very different, it’s accessing a class level static method (you don’t have to type the whole class path when accessing that method), not an instance method as in the first two.