scotthmurray / d3-book

Code examples for “Interactive Data Visualization for the Web”
http://d3book.com
Other
2.4k stars 1.79k forks source link

Anonymous(?) function passed to keys().value() given a name #29

Open Selbosh opened 6 years ago

Selbosh commented 6 years ago

In Chapter 16, it's not clear why the anonymous function passed to stack.keys(keys).value() is given a name. Line 117 reads:

https://github.com/alignedleft/d3-book/blob/4cbba7c74ebd29e6a07ec25150bf0ddb6e108112/chapter_16/01_initial_chart.html#L117

The full block is

//Tell stack function where to find the keys
stack.keys(keys)
    .value(function value(d, key) {
        return d[key].sales;
    });

But this is functionally equivalent (having checked the console.log()) to

stack.keys(keys)
    .value(function (d, key) {
        return d[key].sales;
    });

(And of course in ES6:)

stack.keys(keys)
    .value((d, key) => d[key].sales);

So I'm not sure if this is a mistake, an inconsistency in style, or something subtle that I am missing (but in which case should be documented.)

This appears on page 336 of the 2nd edition of the book.

scotthmurray commented 6 years ago

@Selbosh hey, great catch. I think it's just a (rare!) stylistic inconsistency. I will accept the fact that you noticed it as evidence that I was highly consistent throughout the book otherwise. See, you were really writing in with a compliment! 😄

Thanks for catching this. I'm going to leave this open, as I'd like to amend this with the next update to the book (though the code will remain unchanged for now).