I find myself lacking knowledge around the below.
Take this class:
public class MyClass
{
public var width:int = 10;
public var height:int = 10;
public function MyClass(width:int, height:int)
{
trace(width, height);
}
}
The output will always be the supplied values for width and height, rather than the width and height properties that belong to MyClass. You will never receive an error for the above, either, even though it seems like there should be one for conflicting property names.
Why/how does this work? Also, are the width and height defined above my constructor still somehow accessible within my constructor?
It works, because the parameter names “shadow” the field variable names, i.e. the compiler assumes you meant those.
You can access the members by using
this.widthandthis.height. This is also the de facto syntax the compiler converts all implicit member calls to in byte code (if there were no local variable of the same name,widthwould automatically be translated tothis.width).BTW some IDEs, like FDT, allow you to set a warning or error message for name shadowing.