If I have some xml like so:
<books>
<book title="this is great" hasCover="true" />
<book title="this is not so great" />
</books>
What's the best (or accepted) way in actionscript to check if the hasCover attribute exists before writing some code against it?
Just to add some precisions.
If you want to check if the property exists even though it's empty you should definitely use hasOwnProperty :
var propertyExists:Boolean = node.hasOwnProperty('@hasCover');
Checking the length of the content is somehow dirty and will return false if the value of the attribute is empty. You may even have a run-time error thrown as you will try to access a property(length) on a null object (hasCover) in case the attribute doesn't exist.
If you want to test if the property exists and the value is set you should try both starting with the hasOwnProperty so that the value test (eventual run-time error) gets ignored in case the attribute doesn't exist :
var propertyExistsAndContainsValue:Boolean = (node.hasOwnProperty('@hasCover') && [email protected]());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With