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 8035153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:13:44+00:00 2026-06-05T02:13:44+00:00

I am struggling to understand how this JavaScript code work. I am learning JS,

  • 0

I am struggling to understand how this JavaScript code work. I am learning JS, and not exposed to a dynamic, functional language before. So, I visualize function calls in bit procedural, hierarchical order. With d3.js, one can draw svg elements as explained here


var dataset = [ 5, 10, 15, 20, 25 ];

d3.select("body").selectAll("p")
    .data(dataset)
    .enter()
    .append("p")
    .text("New paragraph!");

Let’s change the last line:

.text(function(d) { return d; });

Check out what the new code does on this demo page.

Whoa! We used our data to populate the contents of each paragraph, all thanks to the magic of the data() method. You see, when chaining methods together, anytime after you call data(), you can create an anonymous function that accepts d as input. The magical data() method ensures that d is set to the corresponding value in your original data set, given the current element at hand.


This magic, mentioned above is what I fail to understand. “d” is not a global variable, as if I change to another (c) name, it still works. So, the data method may be setting the value for the anonymous fn.

But, typically(with my limited reading) chaining is possible because the current function returns an object, on which the next method can be invoked. In the above case, how data method knows whether a text (“New paragraph!”) is passed by the user, otherwise pass the data to the anonymous fn. The doubt is, the text method is down the line and data() is already executed. How the data is passed to the anonymous function?

thanks.

  • 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-05T02:13:46+00:00Added an answer on June 5, 2026 at 2:13 am

    Digging into d3.js internals shows the following result for text function:

    d3_selectionPrototype.text = function(value) {
      return arguments.length < 1
          ? this.node().textContent : this.each(typeof value === "function"
          ? function() { var v = value.apply(this, arguments); this.textContent = v == null ? "" : v; } : value == null
          ? function() { this.textContent = ""; }
          : function() { this.textContent = value; });
    };
    

    In case the supplied argument is a function, the following code gets executed:

    this.each(function() {
        var v = value.apply(this, arguments);  // executing function provided
        this.textContent = v == null ? "" : v;
    });
    

    Function each is declared as:

    d3_selectionPrototype.each = function(callback) {
      for (var j = -1, m = this.length; ++j < m;) {
        for (var group = this[j], i = -1, n = group.length; ++i < n;) {
          var node = group[i];
          if (node) callback.call(node, node.__data__, i, j); // this is the line you are interested in
        }
      }
      return this;
    };  
    

    so on each invocation it supplies an element from this. And, getting down to it, this is populated by data function invocation.

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

Sidebar

Related Questions

Hi guys I was looking at this code but I'm struggling to understand what
I'm doing a dive into JavaScript, and I am struggling to understand 'this' references
I'm struggling to understand why does this doesn't work. df <- data.frame(a=1:10, b=1:10) foo
I am struggling to understand on java threads work so excuse this rather simple
The below code is taken from http://bonsaiden.github.com/JavaScript-Garden/ function Foo() { this.value = 42; }
I am really struggling to understand why, when I change my code to use
I'm unfamiliar with Javascript and have been struggling to understand how I'd go about
I'm struggling to understand the difference of the following 2 sets of code. The
I'm sure this shouldn't be so hard! I've been struggling with facebook's javascript :
I'm trying to understand pure prototype-based JavaScript and one specific thing I'm struggling with

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.