• 13
• 18
• 19
• 27
• 10

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

This topic is 4390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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)?

##### Share on other sites
Quote:
 Original post by DakeDesuWhat 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 DakeDesuDoes 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.

##### Share on other sites
Quote:
 Original post by DakeDesuI 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).

Quote:
 Does DOM being an "interface" (if that is what it is), make it so that I don't copy nodes

No. DOM is an "interface" because it's a set of properties / methods which a DOM document has - it's not defining an implementation. There are typically several different DOM implementations in the same browser for different types of document (HTML and XML anyway).

DOM objects behave like any other object in javascript - they are passed around by reference. No copies are made unless you do so explicitly.

Mark

##### Share on other sites
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.