Recently when working on a project in javascript I tried something similar to this snippet. I was surprised to find out it doesn't work and instead throws an error.
const test = [1, 2, 3, 4];
const something = [1, 2, 3, 4, ,5, 6, 7, 8].filter(test.includes);
console.log(something);
TypeError: Cannot convert undefined or null to object
at includes (<anonymous>)
at Array.filter (<anonymous>)
at evalmachine.<anonymous>:3:45
at Script.runInContext (vm.js:74:29)
at Object.runInContext (vm.js:182:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:180:13)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
Is this a javascript bug or am I misunderstanding something here. The following snippets work fine:
const test = [1, 2, 3, 4];
const something = [1, 2, 3, 4, ,5, 6, 7, 8].filter(item => test.includes(item));
console.log(something);
And:
const test = [1, 2, 3, 4];
const someFunc = theThing => test.includes(theThing);
const something = [1, 2, 3, 4, ,5, 6, 7, 8].filter(someFunc);
console.log(something);
Working towards a more functional style of programming and point free when I see the patterns easily enough, this seems like an inconsistency.
Edit: This is not a duplicate. I don't need clarification on This
, just on how it was handled in the context of the includes function specifically.