The example of includes of data in ES6 is incomprehensible. There are no parameters when defining, but the call can pass parameters.

related codes

const contains = (() =>
  Array.prototype.includes
    ? (arr, value) => arr.includes(value)
    : (arr, value) => arr.some(el => el === value)
)();

contains(["foo", "bar"], "baz"); // => false

this is an example I saw in Ruan Yifeng"s ES6 tutorial. I can"t understand how the parameters are passed when the function is called.

Jul.14,2021

this code is too refined. Let's extract the key points:

 
The contains function defined by 

returns one function and defines two parameters;

if the original method of the array has a method of includes, it directly returns (arr, value) = > arr.includes (value) where two parameters arr, value;

are defined.

if there is no includes method, (arr, value) = > arr.some (el = > el = value) is returned, where two parameters arr, value;

are also defined.

and there are anonymous expressions outside contains that (), is executed immediately;

when calling contains (['foo',' bar'], 'baz'), it is equivalent to passing [' foo', 'bar'] to arr,' baz' to value.

Menu