array.map(function(currentValue, index, arr), thisValue)
array.filter(function(currentValue, index, arr), thisValue)
why is the use of thisValue?
array.map(function(currentValue, index, arr), thisValue)
array.filter(function(currentValue, index, arr), thisValue)
why is the use of thisValue?
Because of how the this
keyword works, you'd lose the context when you do something like:
array.map(this.callback)
The workaround would be:
array.map(this.callback.bind(this))
The thisValue
is a more elegant way to do that:
array.map(this.callback, this)
And you can use it for your own purposes if you want, this
doesn't need to be the original this
:
/**
* Accepts a callback and returns the array mapped over this callback.
* The `this` context inside the callback will be set to someElement,
* you can use it to influence your callback's return value.
*/
function foo(callback) {
return array.map(callback, someElement)
}