Flash 如何检查对象在命名空间中是否有属性?
我想检查对象在名称空间中是否有已定义的成员。如果试图将Flash 如何检查对象在命名空间中是否有属性?,flash,actionscript-3,Flash,Actionscript 3,我想检查对象在名称空间中是否有已定义的成员。如果试图将hasOwnProperty方法与QName一起使用,但不支持: package { import flexunit.framework.Assert; public class ObjectTest extends Object { public namespace some_public_namespace; some_public_namespace var definedMemb
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 );
}
}
}
}
据我所知,最好的办法是尝试访问该物业 例如,执行以下操作的实用程序函数:
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"));
据我所知,最好的办法是尝试访问该物业 例如,执行以下操作的实用程序函数:
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"));