Difference between revisions of "Avoiding Casting"

From PowerUI
Jump to: navigation, search
(Convenience functions)
(NodeList)
Line 64: Line 64:
 
=== NodeList ===
 
=== NodeList ===
  
*E.g. childNodes uses these*
+
''E.g. childNodes uses these''
  
 
{| class="wikitable"
 
{| class="wikitable"

Revision as of 22:12, 9 February 2017

PowerUI 2 has a standardised DOM. That means that methods like getElementById return an Element rather than the more useful HtmlElement, which results in a lot of casting that previously wasn't required.

Why does it happen at all?

HTML5 can have content like SVG and MathML embedded within it; an SvgElement is not a HtmlElement. However, the vast majority of the time, you'll be interacting with a HtmlElement (and on the web, Javascript hides this from you).

Convenience functions

If you find an instance where a convenience function would be useful, please let us know!

Document

(these ones have the bonus of being a little shorter too - just omit the word Element from the standard Web API)

Standard name Convenience name What it does
getElementById getById Element to HtmlElement
getElementByTagName getByTagName Element to HtmlElement
getElementByAttribute getByAttribute Element to HtmlElement

Event

Standard name Convenience name What it does
target htmlTarget Element to HtmlElement

Element

Standard name Convenience name What it does
parentNode htmlParentNode Node to HtmlElement
document htmlDocument Document to HtmlDocument

NodeList

E.g. childNodes uses these

Standard name Convenience name What it does
get getHtml Node to HtmlElement