0
const i = [1, 2, 3]
const j = [3, 4, 5]

i.some(j.includes)

produces Uncaught TypeError: can't convert undefined to object

Whereas doing

const k = element => j.includes(element)
i.some(k)

works as expected.

Why is that?

laur
  • 498
  • 9
  • 17
  • 2
    There are two reasons: The first reason is that even though you aren't using `this` in *your* code, `this` has the wrong value when `includes` is called. There is no intrinsic link between `j` and `includes` (which it inherits from `Array.prototype`), `includes` needs to be called with the correct `this` in order to know which array to search through. [`some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) calls the function you pass it with the value of `some`'s second argument (`undefined` if you don't pass one). – T.J. Crowder Aug 15 '22 at 08:12
  • 1
    The second reason is that `some` passes multiple args to `includes` (the element, its index, and the array you called `some` on), and [`includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#syntax) accepts up to two params: The value to search for, and the index to start searching from. So even if you did `i.some(j.includes, j)`, it still wouldn't work, because even though that would call `includes` with the correct `this`, `includes` won't see the `3` because the call for the `3` will have the index `2`, and `3` is earlier in `j` than that. – T.J. Crowder Aug 15 '22 at 08:14
  • 1
    (More about that second problem in [this question's answers](http://stackoverflow.com/questions/262427/javascript-arraymap-and-parseint).) – T.J. Crowder Aug 15 '22 at 08:15

0 Answers0