0

What is the use of this following code and what is the expected behavior.

Using bind call and apply in a very complicated way

var test = Function.prototype.call.bind(Function.prototype.bind,Function.prototype.call)
var mapGet = test(Function.prototype.apply);
var arrayIndex = test(Array.prototype.slice);
Connor
  • 33
  • 1
  • 6

1 Answers1

0

Chances are that this code has been intentionally obfuscated by the author.

arrayIndex works the same way as Array.prototype.slice does and has the following syntax:

arrayIndex(arr, startIndex, [endIndex])

mapGet is a function that can be called as follows:

mapGet(fn, context, arr)

it returns the result of calling fn with the arguments passed in the array arr using the given context

var test = Function.prototype.call.bind(Function.prototype.bind,Function.prototype.call)
var mapGet = test(Function.prototype.apply);
var arrayIndex = test(Array.prototype.slice);

console.log(
  arrayIndex([1, 2, 3, 4], 1, 3) // [2, 3]
);

console.log(
  mapGet((x, y) => x + y, null, [10, 20]) //  30
);
antonku
  • 7,377
  • 2
  • 15
  • 21