[web] How it works :: DOM as an interface

Started by
2 comments, last by DakeDesu 18 years, 1 month ago
Okay, something has been bugging me lately. I know that if I were to do: var node = document.getElementById['blah']; node.setAttribute('pie','floorCAEK'); It would set that node's attribute, pie, to be of the value floorCAEK. Is this because XML has to have each IDREF value a unique value, or is it due to this term "interface" I keep seeing when I use the W3C Recommendations as reference? What I am afraid of is a code like `var child = node.firstChild;` making two instances of whatever is contained by `node.firstChild` Does DOM being an "interface" (if that is what it is), make it so that I don't copy nodes (unless I do `node.cloneNode()` of course), or am I wrong and DOM works like most other OOP implementations (Where a reference is made of the object when an assignment operator is used on it, and references to that object until one of them is changed)?
[ Six Hour Game Contest | ( Thread | Blog | Wiki | 6hour Bio ) ][ Website | (Blog | Gallery ) ]
Advertisement
Quote:Original post by DakeDesu
What I am afraid of is a code like `var child = node.firstChild;` making two instances of whatever is contained by `node.firstChild`


No copy will be made, in &#106avascript you're working with references. If you modify 'child' after the assignment, you're also modifying 'node.firstChild'.

Quote:Original post by DakeDesu
Does DOM being an "interface" (if that is what it is), make it so that I don't copy nodes (unless I do `node.cloneNode()` of course), or am I wrong and DOM works like most other OOP implementations


Umm, right and wrong [smile] OOP is a design paradigm, not an 'implementation' as such. DOM (Document Object Model) makes use of an object-oriented design, but that's really got nothing to do with whether or not you're passing around copies or references of a node.

Quote:Original post by DakeDesu
(Where a reference is made of the object when an assignment operator is used on it, and references to that object until one of them is changed)?


If I'm reading you correctly the term you're thinking of here is 'copy-on-write semantics' which is not the 'normal' use of references, but can be used as an optimisation if appropriate.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Quote:Original post by DakeDesu
I know that if I were to do:
var node = document.getElementById['blah'];
node.setAttribute('pie','floorCAEK');
It would set that node's attribute, pie, to be of the value floorCAEK.


No, it wouldn't. You'd have to use round brackets for getElementById("blah") not square ones [].

getElementById is a method not an array/object.

Quote:
Is this because XML has to have each IDREF value a unique value, or is it due to this term "interface" I keep seeing when I use the W3C Recommendations as reference?


Neither.

Quote:
What I am afraid of is a code like `var child = node.firstChild;` making two instances of whatever is contained by `node.firstChild`


As mentioned above, objects are passed by reference in &#106avascript. This is good, and right, and does what we expect usually (for Java/C# programmers, anyway).<br><br><!--QUOTE--><BLOCKQUOTE><span class="smallfont">Quote:</span><table border=0 cellpadding=4 cellspacing=0 width="95%"><tr><td class=quote><!--/QUOTE--><!--STARTQUOTE--><br>Does DOM being an "interface" (if that is what it is), make it so that I don't copy nodes<br><!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>No. DOM is an "interface" because it's a set of properties / methods which a DOM document has - it's not defining an <strong>implementation</strong>. There are typically several different DOM implementations in the same browser for different types of document (HTML and XML anyway).<br><br>DOM objects behave like any other object in &#106avascript - they are passed around by reference. No copies are made unless you do so explicitly.<br><br>Mark
Ah, I think I got you now... this was a little thing to expect in EMCAscript that I wouldn't expect elsewhere (as I normally deal with copy on write).

As per the square braces... I simply didn't test the code before I posted it. I've rarely been making syntax errors lately and I got wreckless(wrong word, I know).

Thank for your help.

It appears I got some definitions wrong, and you guys set me straight.
[ Six Hour Game Contest | ( Thread | Blog | Wiki | 6hour Bio ) ][ Website | (Blog | Gallery ) ]

This topic is closed to new replies.

Advertisement