[web] How it works :: DOM as an interface
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)?
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 javascript 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.
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 javascript. 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 javascript - 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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement