myBind.js 1.0 KB

1234567891011121314151617181920212223242526
  1. const myBind = (boundTargetFunction, boundThis, boundArguments) => {
  2. return (...newBoundArguments) => {
  3. const iterator = newBoundArguments.values();
  4. let updatedArr = [];
  5. boundArguments.forEach((element, index, thisArr) => {
  6. element === undefined ? updatedArr.push(iterator.next().value) : updatedArr.push(thisArr[index]);
  7. });
  8. return boundTargetFunction.apply(boundThis, updatedArr);
  9. }
  10. }
  11. let pow5 = myBind(Math.pow, Math, [undefined, 5]);
  12. let cube = myBind(Math.pow, Math, [undefined, 3]);
  13. console.log(pow5(2)); //32
  14. console.log(cube(3)); //27
  15. let chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9])
  16. console.log(chessMin(-1,-5,3,15)); //-5
  17. let zeroPrompt = myBind(prompt, window, [undefined, "0"]);
  18. let someNumber = zeroPrompt("Введите число"); // вызывает prompt("Введите число","0")
  19. let joinStr = myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d')
  20. console.log(joinStr) // abcdef