Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:08:51+00:00 2026-06-11T16:08:51+00:00

Below is code from Douglas Crockford’s The Good Parts. For most part the code

  • 0

Below is code from Douglas Crockford’s The Good Parts.

For most part the code makes sense. Except, I don’t understand this line here:

var walk_the_DOM = function walk(node, func) {

as it appears the function is given two names – walk_the_dom() and walk()

Further down you can see the code is actually called both ways so that indeed both of these names reference the function.

Why is this function given two names?

// Define a walk_the_DOM function that visits every
// node of the tree in HTML source order, starting
// from some given node. It invokes a function,
// passing it each node in turn. walk_the_DOM calls
// itself to process each of the child nodes.

var walk_the_DOM = function walk(node, func) {
    func(node);
    node = node.firstChild;
    while (node) {

        // walk() called here

        walk(node, func);
        node = node.nextSibling;
    }
};

// Define a getElementsByAttribute function. It
// takes an attribute name string and an optional
// matching value. It calls walk_the_DOM, passing it a
// function that looks for an attribute name in the
// node. The matching nodes are accumulated in a
// results array.

var getElementsByAttribute = function (att, value) {
    var results = [];

   // walk_the_DOM() called here

    walk_the_DOM(document.body, function (node) { 
        var actual = node.nodeType === 1 && node.getAttribute(att);
        if (typeof actual === 'string' &&
                (actual === value || typeof value !== 'string')) {
            results.push(node);
        }
    });

    return results;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:08:52+00:00Added an answer on June 11, 2026 at 4:08 pm

    This is in order for the recursion to work safely, I would imagine.

    For example, if you wanted to only use the walk_the_DOM name, the variable could be reassigned later on, or not accessible due to the scope, so it is not safe to use it inside of the function itself.

    UPDATE:

    I’ve done some research, and here’s what I found. First of all, refer to ECMAScript 5 specification, section 13.
    There are two ways to define a function: a) using FunctionDeclaration, and b) using FunctionExpression.

    They look very similar, but are also somewhat different. The following is a FunctionDeclaration:

    function f() { };
    

    These two are both FunctionExpression:

    var x = function() { };
    var y = function f() { };
    

    The interesting part for us is about FunctionExpression. In case of var y = function f() { }, the identifier f is only visible from inside the function body. In other words, anywhere outside of { }, typeof f will return undefined.

    Now it’s time for some practical examples. Let’s say we want to write a recursive function using FunctionDeclaration:

    function f1(x) { x > 5 ? console.log("finished") : f1(x + 1) };
    

    Now we want to “copy” the function to another variable and to set f1 to something else:

    var f2 = f1;
    var f1 = function() { console.log("Kitteh") };
    

    But this doesn’t work as expected:

    f2(1); // outputs: Kitteh
    

    Now if you used the Douglas Crockford’s way of defining a recursive function:

    var f1 = function myself(x) { x > 5 ? console.log("finished") : myself(x + 1) };
    

    This way you can reassign the function to any variable as many times you want. At the same time, we ensured that the function always calls itself, and not some function assigned to the variable f1.

    So the answer to the initial question: you define recursive functions in this manner, because it is the most flexible and robust way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am striving to follow Douglas Crockford's advice from JavaScript: The Good Parts and
I'm reading Douglas Crockford's book JavaScript: The Good Parts. I can't understand the sentence
The below code is almost identical to some code from Douglas Crockford's superb book
I don't know where I've got a mistake, when I fire below code from
I am trying to understand below code from andengine to load the texture, i
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane.
I am creating a random ID using the below code: from random import *
HI: Below code is from RequestFactoryEditorDriver: /** * Returns a new array containing the
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,
Below is the code from a plugin I use for sitemaps. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.