Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    is appendChild() appending a reference to an object or a copy

    If I do appendChild(obj), is that a refernce to an object or a copy of that object?

    If it was a reference, then if i did this in sequence:

    parent.appendChild(obj);
    obj = something_else;

    Then in theory parent would have a child that is equal to "something_else" however if it was a copy, I could just reusing the obj object for something else just as.

    obj = something_else_again;

    and parent would still have a child that is equal to something_else.

    Which one is it?

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Reference -- but you're a bit off on your thinking..

    obj is a reference to an object, a pointer if you will. parent.appendChild(obj) appends the object as a child of parent, the object obj referred/pointed to.

    obj = something_else; doesn't delete object, but has obj reference a different object: something_else. The first object obj referred to, still has a reference: it's one of the childNodes of parent.

    I know it's sort of confusing, but that's the way it works. An object doesn't disappear (get garbage collected) until it has lost all references to itself. In your example, there's still one reference to it.

    If that doesn't make sense, post back and tell me what you do understand.. if I'm incorrect, please somebody correct me.

    Sadiq.

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I do understand, I've done a good deal of programming. But DOMs for whatever reason is really confusing me, maybe it is that one additional layer of referencing that I kind of see in DOM which is throwing me off. It's like when I think I've finally gotten to the object itself, I actually only got to the reference to an object.

    So is it safe to say then that appendChild(obj) appends the object and not the reference. Because if it appends the reference, whenever that reference points to something different then the appended child will be something different.

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, it appends the object. See? You've already got it!

    It is tricky though, I agree. Sometimes I don't know if I've got the right object or what's going on.. and when you got to change something, oh man!! But it keeps you sharp

    Happy coding,
    Sadiq.

  • #5
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sad69,
    Maybe you have a good suggestion to test what you have a handle on. I am just using alert(), but in the old days, with java or C++, if I had a handle on the reference I would get back with system.out.println():

    ajsoadkj232r4 or something like that

    or "hello world" if it was the actual object.

    But with alert() half the time I just get undefined. And I don't know what that really means. Is there a better way to check?

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not really.. if I get undefined, I just just go back up one level and see where I'm at. After a while of that if I'm still lost, I just step back and re-evaluate what I'm trying to do.

    I use paper and pencil for lots of stuff. innerHTML is a good one to alert() when appropriate. When you were working with Java and C++, did you ever work with trees? Binary or n-ary or something? Because that's what DOM is. It's an n-ary tree or Nodes. Nodes can be Elements....

    When working with Java, I'm sure you know about APIs. Here's a good one that I use for DOM (since DOM uses Javascript and APIs as a result..):
    http://krook.org/jsdom/Node.html

    Hope that helps,
    Sadiq.

  • #7
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that does help. I was wondering why there wasn't a readily available javascript DOM API, even if it were specific to specific browsers, I would like to see something.

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Now I think you've confused things... the DOM is an API. The ECMAScript bindings are standardised, if you want to see the specification for them you can have a look at <http://www.w3.org/DOM/DOMTR>. Not all browsers behave according to the specification, however, and some browsers add proprietary extensions to it. A prime example of this is innerHTML, a proprietary extension added by iew that all browsers support for compatibility reasons, in difference to outerHTML.

    As for browser implementation documentation, MSDN has a very complete and accurate one for iew (not so for iem). The Gecko DOM documentation is both old and incomplete, but it's still pretty good. Both of these are linked from my sig. (MSDN: DHTML and Moz: DOM, respectively)

    Opera and Safari behaves pretty much according to the specification for what they support. Safari documentation is sparse, but Opera has a nice standards compliancy report.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •