Yes, I'm sorry but that JavaScript example code is terrible.
I realize this sounds harsh but truthfully, it made me immediately disregard the library. If this is how you would solve these issues in JavaScript I'm not taking you seriously.
The web API offers an elegant separation of content, style and logic and these examples are just stuffing everything into logic. Don't do this