Sign in to follow this  
DakeDesu

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

Recommended Posts

DakeDesu    100
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 this post


Link to post
Share on other sites
joanusdmentia    1060
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.

Share this post


Link to post
Share on other sites
markr    1692
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).

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 this post


Link to post
Share on other sites
DakeDesu    100
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this