E4X : 속성의 존재를 가장 잘 점검하는 방법은 무엇입니까?
-
10-07-2019 - |
문제
길이 () 메소드로 할 수 있다는 것을 알고 있습니다.
>x = <a attr1='33' />
>x.@attr1
33
>x.@attr1.length()
1
>x.@attr2.length()
0
그래서 사용할 수 있습니다
if (x.@someattr.length() > 0)
{
.... do something ....
}
그러나 더 적절한 방법이 있습니까?
해결책
신경 쓰지 마세요, 나는 ECMA-357 표준, 특히 xml.prototype.* 및 xmllist.prototype.* 섹션 13.4 및 13.5.
그건 hasOwnProperty()
방법:
js>x = <a attr1='33' ><item>gumball!</item></a>
<a attr1="33">
<item>gumball!</item>
</a>
js>x.@attr1
33
js>x.hasOwnProperty('@attr1');
true
js>x.hasOwnProperty('@attr2');
false
js>x.hasOwnProperty('item');
true
js>x.hasOwnProperty('mongoose');
false
다른 팁
가장 쉬운 방법:
(@attr1 in theXML)
ID attret가 존재하고 그렇지 않으면 거짓이 있으면 TRUE가 반환됩니다.
제휴하지 않습니다 StackOverflow