arr.filter(callback[, thisArg])
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
So, this is the example in the documentation that I am supposed to understand.
Arr = some array, great. filter = a method the array has, great. callback = some function, basically. Great. What is [, thisArg]
though?
Looking at some example on stackoverflow shows this:
objects.filter(function (obj) {
return obj.someIntProp <= 1000
});
I understand how this works, I don't need help using it. I just want to understand how [, thisArg]
is supposed to tell me that the function takes each object in an array. I feel like I am missing something obvious that most people get taught in Coding 101.
The thisArg
parameter is optional. If you pass it, then when the runtime invokes your callback, it'll set this
to whatever that value is.
It's not useful in very many situations, but it's one of those things that is handy when you do want it. An alternative of course would be to bind the callback to whatever you'd pass for thisArg
.
The MDN site by convention uses the [, thisArg]
notation to mean that the parameter is optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With