DOM object augmentation / Dopplegangers / Code Kata

P

Peter Michaux

Hi,

A few times, I've programmed a web page where I create JavaScript
objects to mirror DOM objects. I think of the JavaScript objects as
duals or dopplegangers where the JavaScript and DOM objects are
intimately connected. I've seen this called a backing object in one
book on JavaScript.

Lately I've been thinking about augmenting the DOM objects directly
with new properties. If I do augment, I think this means I have to do
a cleanup on the window.onunload even for Internet Explorer memory
leaks. Are there any other problems or issues with augmenting DOM
objects? Do people have opinions if this a good or bad practice? Seems
like DOM augmentation is a tidier concept.

Below are examples of each technique for the same task (using
JavaScript 1.6's map function). Any thoughts or suggestions? I know
this can be done in a functional programming style but I'm interested
in comparing the more OOP oriented techniques. However if someone
want's to post alternative techniques I'd be interested to read them.
This is a bit of a code kata (yes a new buzz phrase someone will pick
on) I've been thinking about lately. <URL: http://
codekata.pragprog.com/>

Thank you,
Peter

== using a dual object for the list ==

<ul id="todoList">
<li class="medium">Buy milk</li>
<li class="high">Ask Q on c.l.j</li>
<li class="high">Climb rocks</li>
</ul>

<p><a href="#" id="showHighs">high priority items</a></p>

<script type="text/javascript">

var list = {
el: document.getElementById('todoList'),

getHighPriorityItems: function() {
var items = this.el.childNodes,
highs = [];
for (var i=0; i<items.length; i++) {
if (items.className == "high") {
highs.push(items);
}
}
return highs;
}

};

document.getElementById('showHighs').onclick = function() {
var highs = list.getHighPriorityItems();
alert(highs.map(function(item){return
item.innerHTML;}).join('\n'));
return false;
};

</script>




== augmenting the DOM element ==

<ul id="todoList">
<li class="medium">Buy milk</li>
<li class="high">Ask Q on c.l.j</li>
<li class="high">Climb rocks</li>
</ul>

<p><a href="#" id="showHighs">high priority items</a></p>

<script type="text/javascript">

document.getElementById('todoList').getHighPriorityItems =
function() {
var items = this.childNodes,
highs = [];
for (var i=0; i<items.length; i++) {
if (items.className == "high") {
highs.push(items);
}
}
return highs;
};

document.getElementById('showHighs').onclick = function() {
var highs =
document.getElementById('todoList').getHighPriorityItems();
alert(highs.map(function(item){return
item.innerHTML;}).join('\n'));
return false;
};

</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top