Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28457

Re: Recursive function to find a node in the DOM

From "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it>
Newsgroups comp.lang.javascript
Subject Re: Recursive function to find a node in the DOM
Date 2015-07-21 12:30 +0200
Organization A noiseless patient Spider
Message-ID <mol6so$48v$1@dont-email.me> (permalink)
References <mo53os$am9$1@dont-email.me> <4508534.oS5OQyzpKv@PointedEars.de> <4b1cb0ba-f0b0-4b25-a1aa-692c30e69833@googlegroups.com>

Show all headers | View raw


Il 07/19/2015 12:59 AM, Scott Sauyet ha scritto:
> Thomas 'PointedEars' Lahn wrote:
>> danca (Daniele Campagna) wrote:
>>
>>> I wrote this small recursive function to find a node (having a name) in
>>> a DOM element:
>>>
>>> //usage: nodeFind (element to inspect (object), f.e. a form, name of the
>>> element to search for, 3rd parameter empty at first call)
>>>
>>> function nodeFind(el,n,retval){
>>>    if (el.name==n) {
>>>      return (el)
>>>    } else {
>>>      if (el.hasChildNodes) {
>>>        for (var z=0;z<el.childNodes.length;++z){
>>>          var a=el.childNodes.item(z);
>>>          retval=nodeFind(a,n,retval)
>>>
>>>      }
>>>    }
>>>    return retval
>>> //^^^^^^^^^^^^ note!
>>> }
>> [ ... ]
>>
>>> One strange thing is the way recursion is implemented in javascript: I
>>> did not put the "return retval" statement at first, it seems unuseful,
>>> and went crazy before discovering that it is needed.
>
> That is not really an issue with recursion.  This has to do with the way
> Javascript passes values to functions.  It uses a specialization of call
> by value, sometimes known as "call by sharing".  The reference the caller
> has stored as `retval` is not passed to the function.  Instead a *copy*
> of that reference is passed.  Both refer initially to the same object.
> But when, inside the function, you reassign the new reference, the caller's reference is left alone.  This shouldn't be too surprising if you've dealt
> with languages like Java, Ruby, Python, or even Scheme; they all use a
> similar technique.
>
>
I should have written "the way values are passed to functions".
Unfortunately I don't know any of the languages you are referring to.
In xBase (supposing to have an object mirroring the ECMAscript objects, 
and there is none in fact - but it should be possible to create one), I 
could have written:

element=nodeFind(el,n)

function nodeFind(el,n)(
  local z,a
  if el:name==n
   return (el)
  else
   if el:hasChildNodes
    for z=0 to el:childNodes:length
     a=el:childNodes:item(z)
     nodeFind(a,n)
    next z
  endif
return nil

This function (untested) should return the object (if found) or continue 
to call itself and if no match is found the final return value should be 
NIL.

Anyway thans for the explanation, that casts some light on the issue.

>> There is no "javascript".  [ ... ]
>
> Keep tilting at that windmill.  The rest of us know better.
>

:-)

>
> Recursion is logically correct here.  If you're working with modern browsers,
> you'll have at least a 20000-call deep recursion stack..  If you have a tree
> that deep in your document, heaven help you.  If you have to support IE6 - IE8,
> you'll still get well over 1000 calls deep.  I don't know about you, but I've
> never written a document with that much nesting.
>
> A recursive solution is clear and elegant.  A non-recursive version would be
> much less so.  If you make it tail-recursive, when browsers start to optimize
> tail-recursion, the little performance penalty you would currently pay for
> recursion will be eliminated.
>
>    -- Scott
>

"logically correct" is a part of your answer i like very much, but "a 
recursive solution is clear and elegant" is what really made my day ;-)

Dan
-- 
"Everybody should pay taxes with a smile"
I tried, but they want money.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 09:59 +0200
  Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 11:59 +0200
    Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-15 12:03 +0200
    Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-15 13:36 +0200
      Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-19 12:27 +0200
        Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-19 12:34 +0200
        Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 12:08 +0200
          Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-25 14:11 +0200
            Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-25 21:22 +0200
              Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-27 16:12 +0200
    Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-08 04:50 +0200
      Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-08 09:33 +0200
        Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-12 00:48 +0200
          Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-12 01:18 +0200
            Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-13 23:26 +0200
              Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-16 08:48 +0200
                Re: Recursive function to find a node in the DOM "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-08-20 02:11 +0200
  Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-18 15:59 -0700
    Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 12:30 +0200
      Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-21 13:53 +0200
        Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 14:51 +0200
          Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-07-21 16:05 +0200
            Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-07-21 20:35 +0200
            Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-30 22:10 -0700
              Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-06 10:07 +0200
                Re: Recursive function to find a node in the DOM Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-06 20:15 +0200
                Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-08 01:21 +0200
        Re: Recursive function to find a node in the DOM Scott Sauyet <scott.sauyet@gmail.com> - 2015-07-30 22:09 -0700
  Re: Recursive function to find a node in the DOM Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-29 03:04 +0000
    Re: Recursive function to find a node in the DOM "danca (Daniele Campagna)" <cyberdanny@tiscalinet.it> - 2015-08-06 10:20 +0200

csiph-web