-5

I am following some tutorials and I came up with the following code, I am unable to understand how is it working behind the scene. Anyone can please explain how is it actually working.

So here I made a function mapForEach.

const arr1 = [1, 2, 3]
const mapForEach = (arr, fn) => {
    let newArr = []
    for (let i = 0; i < arr.length; i++) {
        newArr.push(
            fn(arr[i])
        )

    }
    return newArr;
}

Now here I made other two Functions CheckPastLimit and CheckPastLimitSpecified. Now here checkPastList check for the condition. All good to this point.

const CheckPastLimit = (limiter, item) => {
    return item > limiter
}

Now here CheckPastLimit is used as a parameter and arr2 return value that pass CheckPastLimit condition (item > limiter). So I want to know how is this line working exactly? How is this line working behind the scene?

const arr2 = mapForEach(arr1, CheckPastLimit.bind(this, 1))
console.log(arr2)
Arsalan Khattak
  • 768
  • 2
  • 8
  • 16
  • Unless I'm very much mistaken, the tutorial writer seems to be unaware of basic functions like `Array#map`, instead re-implementing it themselves... – Niet the Dark Absol Aug 17 '18 at 15:03
  • [documentation MDN bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) – epascarello Aug 17 '18 at 15:03
  • Did you read the documentation of how `bind` works? There can be endless answers to this question. Please ask about specific problem you are facing, instead. – 31piy Aug 17 '18 at 15:03
  • Use of `bind` and `this` makes no sense here - `const CheckPastLimitSimplified = limiter => item => item > limiter` disposes of both and still produces the same result. Better yet, rename the function to `greaterThan` - related: https://stackoverflow.com/a/32787782 – Mulan Aug 17 '18 at 19:44

1 Answers1

0

When we use binding here we implement currying

It means that CheckPastLimit.bind(this, 1) will return new function with passed first argument.

example:

const multiply = (a, b) => a*b;
const multiplyByTwo = multiply.bind(this,2)
mltiplyBuTwo(5)// 5*2=10