PDA

View Full Version : this + extends + scope



kiosk77
July 19th, 2007, 09:08 AM
I'm a bit at a lost with the scope of this when extending a class.

I have a method in Base


public function isDefinedProperty( prop:String ):Boolean {
try {
return this["_"+prop]!=null && this["_"+prop]!="null";
} catch ( err:Error ) {
// nothing - handle after the catch
}
return false;
}
Now I extend it in my class extended


private var _property:String = "isGood";

public function test():void {
trace(isDefinedProperty("property"));
}

This traces false because flash says that _property can not be found, if I define the variable in Base, then it traces true. Is "this" scoped to the based class an ignores inheritance? how can I overcome this?


Thank you.



Senocular, want to go 2 for 2? :)

Krilnon
July 19th, 2007, 09:41 AM
Is "this" scoped to the based class an ignores inheritance?

It doesn't ignore inheritance, you just have to remember that the private access modifier means that only the defining class can see and use the properties that are private. The actual instance will have the _property property, but only methods defined in Extended will be able to see and use the property. If you declare the property as public, or you leave off the access modifier, then you'll be able use isDefinedProperty like you wanted to.

kiosk77
July 19th, 2007, 09:52 AM
Krilnon,

thanks. it appears to only work with public though, private and protected are no go, unless i redefine the method with override in the new extended class

senocular
July 19th, 2007, 10:30 AM
internal should work too, assuming they're defined in the same package