user3502374 user3502374 - 1 year ago 81
Javascript Question

Arbitrary variables in d3.js while function call

Almost understanding multi line d3.js example from

However, below code escapes me.

d3.min(cities,function(c){return d3.min(c.values,function(v){return v.temperature;});}),
d3.max(cities, function(c){return d3.max(c.values,function(v){return v.temperature;});})

Are c and v(I understand how they are being used) just arbitrary name that author decides to use? It could have been 'a' and 'b' ?

Answer Source

As you probably know by now, JavaScript functions can have parameters and arguments:

The letters chosen in your example are arbitrary, and would work as well were they a or b. As @thatOneGuy said, they were probably chosen to stand for "cities" and "values".

That being said, this is not a good practice (despite that being Mike Bostock): when you write code, other people should focus on what you are saying, instead of losing time understanding how are you saying it. In D3 community, we normally use the letter "d", that stands for "data". Other common practice, when sorting, is using the letters "a" and "b", and when referring to an index using the letter "i". This make the code uniform among the D3 community.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download