user3502374 user3502374 - 6 months ago 12
Javascript Question

Arbitrary variables in d3.js while function call

Almost understanding multi line d3.js example from

https://bl.ocks.org/mbostock/3884955

However, below code escapes me.

y.domain([
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

As you probably know by now, JavaScript functions can have parameters and arguments: http://www.w3schools.com/js/js_function_parameters.asp

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.