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

  • Home
  • SEARCH
  • 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 8028369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:07:03+00:00 2026-06-05T00:07:03+00:00

In the source code of d3.layout.force, line 158, there is this code force.charge =

  • 0

In the source code of d3.layout.force, line 158, there is this code

force.charge = function(x) {
    if (!arguments.length) return charge;
    charge = typeof x === "function" ? x : +x;
    return force;
};

Now, if you go to line 225, you will see

charges = [];
if (typeof charge === "function") {
  for (i = 0; i < n; ++i) {
    charges[i] = +charge.call(this, nodes[i], i);
  }
} else {
  for (i = 0; i < n; ++i) {
    charges[i] = charge;
  }
}

What I did not understand here is the line

charges[i] = +charge.call(this, nodes[i], i);

I am new to JavaScript and can not understand what’s going on here.

As far as I understood charge takes only 1 argument (x). Here “this” is passed to give the context of current object but what about the other two? Which one of “nodes[i]” and “i” is taken as “x” ?

Again what is “= +” doing here?

  • 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-05T00:07:05+00:00Added an answer on June 5, 2026 at 12:07 am

    You have to follow charge more carefully. It is variable defined in line 11:

    charge = -30,
    

    The function force.charge which you quoted is for setting the charge, it is not the function referred to in +charge.call(this, nodes[i], i);. Have a look at the second line of force.charge:

    charge = typeof x === "function" ? x : +x;
    

    x can be a function (callback) you pass, to dynamically calculate the charge. The current node (nodes[i]) and the index of the node (i) will be passed to this callback, so that you can calculate the charge dynamically based on these values:

    force.charge(function(node, index) {
        return index * 2;
    });
    

    x (and therefore charge) can also be a number or numerical string. That’s why it is tested beforehand whether charge is a function or not:

    if (typeof charge === "function") {
      // function so we call it and pass the current node and index
    } else {
      // static value, the same for each node
    }
    

    Apert from that, you can always pass any number of arguments to a function, no matter how many parameters it has defined. For example:

    function foo() {
        alert([].join.call(null, arguments));
    }
    
    foo('a', 'b');
    

    will alert a,b.


    To answer your questions: The arguments passed to .call() [MDN] or .apply() [MDN] are passed in the same order to the function. So if I have a function function foo(a, b, c) then foo.call(null, x, y) would pass x as a and y as b (c would be undefined).

    The + operator is the unary plus operator [MDN], which simply converts the operand into a number.

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

Sidebar

Related Questions

To my knowledge, in the ZXing Android source code, there is a custom layout
This is my android xml source code for a map - bottom bar layout.
Are there any winforms source code editor controls available with color coding ? pref.
with this code, my program just force close(error) ***public View x = findViewById(R.string.nfoname);*** @Override
Here is my source code and it keeps force closing everytime I run it...
I keep getting an error with this source code. Does anyone know what im
Suppose I compile a source file file which contains this piece of code, struct
source code ... Thing is, if i specify required metadata using fluent modelmetadata provider
[Source Code] data FooBar = Foo | Bar [Command] $ ghc -c foo_bar.hs foo_bar.hs:1:0:
Superclass source code public class Date { private int month; private int day; private

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.