更新时间:2022-05-07 22:27:04
只是为了增加一些精度.
Just to add some precisions.
如果你想检查属性是否存在,即使它是空的,你绝对应该使用 hasOwnProperty :
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');
检查内容的长度有点脏,如果属性值为空则返回false.您甚至可能会抛出运行时错误,因为您将尝试访问空对象 (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.
如果您想测试属性是否存在并设置了值,您应该尝试从 hasOwnProperty 开始,以便在属性的情况下忽略值测试(最终运行时错误)不存在:
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') && node.@hasCover.length());