4

I was reading implementation of negate function in lodash

https://github.com/lodash/lodash/blob/4.17.11/lodash.js#L10584

function negate(predicate) {
    if (typeof predicate != 'function') {
        throw new TypeError(FUNC_ERROR_TEXT)
    }
    return function() {
        var args = arguments
        switch (args.length) {
            case 0:
                return !predicate.call(this)
            case 1:
                return !predicate.call(this, args[0])
            case 2:
                return !predicate.call(this, args[0], args[1])
            case 3:
                return !predicate.call(this, args[0], args[1], args[2])
        }
        return !predicate.apply(this, args)
    }
}

I want to understand why are they using .call for less number of arguments and then .apply for remaining cases?

Is this for some performance gain? If so , kindly include the source of the info

James Coyle
  • 9,922
  • 1
  • 40
  • 48
ashish singh
  • 6,526
  • 2
  • 15
  • 35

0 Answers0