我想检查对象是否在命名空间中有定义的成员。如果尝试将hasOwnProperty
方法与QName一起使用,但不支持:
package {
import flexunit.framework.Assert;
public class ObjectTest extends Object {
public namespace some_public_namespace;
some_public_namespace var definedMember : String;
[Test]
public function testMemberWithinNamespace () : void {
// this should be elegant way... but actualy it doesn't work
try {
Assert.assertTrue( "Expect hasOwnProperty method work with QName", this.hasOwnProperty( new QName( some_public_namespace, "definedMember" ) ) );
} catch ( error : Error ) {
Assert.assertTrue( "hasOwnProperty method failed to work with QName", false );
}
// this is non elegant way that works
try {
this[ new QName( some_public_namespace, "definedMember" ) ];
Assert.assertTrue( "Expect no error", true );
} catch ( error : Error ) {
Assert.assertTrue( "Expect this line not to be runned", false );
}
try {
this[ new QName( some_public_namespace, "undefinedMember" ) ];
Assert.assertTrue( "Expect this line not to be runned", false );
} catch ( error : Error ) {
Assert.assertTrue( "Expect property doesn't exist", true );
}
}
}
}
答案 0 :(得分:1)
据我所知,最好的方法是尝试访问该物业。
例如,执行此操作的实用程序函数:
public static function propertyIsAvailable(object:Object,
propertyName:Object):Boolean
{
var available:Boolean = false;
try {
var v:* = object[propertyName];
available = true;
} catch (e:Error) {
}
return available;
}
然后,您可以检查公共属性和命名空间属性:
var available:Boolean = propertyIsAvailable(object, "myPublicProperty");
var available:Boolean = propertyIsAvailable(object,
new QName(some_namespace, "myNamespaceProperty"));