July 17th, 2008

AS3 – My Fav Functions – hasOwnProperty()

I need to blog code samples more. To be quite frank, I don't because something in my brain always tells me my posts have to be polished and verbose and I'm always short on time. That's probably why I have a bunch of unfinished drafts sitting in my wordpress db. That being said, I'm going to attempt to short circuit my brain to allow me to post quick snippets and as a result hopefully quicker and more useful blog posts...we'll see how it goes.

So, here's the first attempt. Earlier this week a colleague of mine asked if there was a cool way to E4X filter on attributes without having them cause an RTE. The RTE would be generated in the case of nodes that don't contain the attribute.

Here's a sample block of XML:

XML:
  1. <stuff>
  2.     <item id="1"/>
  3.     <item id="2" someAttrib="hello world"/>
  4.     <item id="3"/>
  5. </stuff>

We want to filter based on @someAttrib, but when you use the E4X predicate filtering syntax, the app will RTE on the first node since it doesn't contain @someAttrib. Due to the RTE, what my colleague was doing was surrounding his code in a Try/Catch as the following example illustrates:

ACTIONSCRIPT:
  1. try
  2. {
  3.     return p_item.( @someAttrib == "hello world" ).toXMLString();
  4. }
  5. catch( e:Error )
  6. {
  7.     trace( "doh! I broke" )
  8. }

My colleague knew there was a better way, since he knows Try/Catch can very slow. So he posed the question. I answered quickly since I got to promote one of my favorite functions in AS3: hasOwnProperty(). Here's the revised code block:

ACTIONSCRIPT:
  1. if( p_item.hasOwnProperty( "@someAttrib" ) )
  2. {
  3.     return p_item.( @someAttrib == "hello world" ).toXMLString();
  4. }

Enjoy!

5 Responses

  1. Guojian Miguel Wu Says:

    or you can simply say
    [code]
    return p_item.attribute("someAttrib");
    [/code]

  2. Guojian Miguel Wu Says:

    Oh, I see what you’re trying to do… here are two one lines way to get the same result.

    p_item.item.( hasOwnProperty(”@someAttrib”) ).toXMLString()

    or

    p_item.item.( attribute(”someAttrib”).length() > 0 ).toXMLString()

  3. Ben Clinkinbeard Says:

    Not sure about performance implications but you can also use the following syntax and it will not throw RTEs:

    myNode.(attribute(”someAttrib”) == “hello world”).toXMLString()

  4. Josh Tynjala Says:

    I was just about to post the same thing Ben did. It’s one of those semi-obscure things about E4X that should be more well known, but isn’t. I had someone point it out to me on my blog too. :)

    There are other useful functions too: child(), children(), elements(), decendants(), comments()… Check the docs for more.

  5. jun Says:

    @[ Guojian, Ben, Josh ]

    You all are rockin’ the additional tips, it’s always good to see other ways of doing things!

    I also made sure to forward these other snippets onto my colleague.

    Thanks again!

    -Jun

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Written Jun

Flex Authority Vol. 1 Issue 2
InsideRIA
Fusion Authority Quarterly Update

Gamer Jun

Categories

Credits

FireStats icon Powered by FireStats