Overview
Sets are a new data type in ES6.
They're similar to Arrays with one major difference, each value must be unique. They have an API that...
For further actions, you may consider blocking this person and/or reporting abuse
This allows you to reuse the callback in several places, without losing the flexibility you have, when defining the callback inline. From a callback that is defined somewhere else, you will loose the ability to look at the object you are currently working.
Use case: transform an array of values to an srray of strings, that have the form `
${value} of ${array.length} items
`.Without the third parameter, you'd need to write a higher order function that takes the length (or the array itself) returns a function that can be used as a callback function:
const createCallback = array => (index, value) => (
value + " of " + array.length + " items"
);
const inputArray = ["foo", "bar"];
console.log(inputArray.map(createCallback(inputArray)));
Code like this is something what I call an "eyebrow raiser". What happens if someone changes the
createCallback
function and mutates the array there (think of the poor guy, who has to debug that... )? You need to take care to have tests in place that protect the code from this kind of change (super edgy edge case that is super dangerous at the same time).More than that! You need it if you chain methods like so:
Otherwise, you'd have to execute and assign these to variables and reach out of the inner scope of the call back function.
Some minor corrections:
foo.add(key, value)
instead offoo.add(key)
in your API overview..values()
. This is so that they provide the same iteration API functions as Maps, which can be useful when writing certain algorithms which will operate on container keys and/or values. Same reason Sets have.entries()
. The important difference being that@@iterator
maps toMap.entries()
butSet.values()
.