lgwebdream / FE-Interview

🔥🔥🔥 前端面试,独有前端面试题详解,前端面试刷题必备,1000+前端面试真题,Html、Css、JavaScript、Vue、React、Node、TypeScript、Webpack、算法、网络与安全、浏览器
https://lgwebdream.github.io/FE-Interview/
Other
6.82k stars 896 forks source link

Day264:请实现 uncurring 完成函数柯里化 #1084

Open Genzhen opened 3 years ago

Genzhen commented 3 years ago
var yideng = Array.prototype.push.uncurring();
(function () {
  yideng(arguments, 4);
  console.log(arguments); // [1,2,3,4]
})(1, 2, 3);

每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案 欢迎大家在下方发表自己的优质见解

二维码加载失败可点击 小程序二维码

扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。


答案

Function.prototype.uncurring = function () {
  var _self = this;
  return function () {
    var obj = Array.prototype.shift.call(arguments);
    return _self.apply(obj, arguments);
  };
};
luuman commented 2 years ago
Function.prototype.uncurring = function() {
  let self = this
  // this 指向push
  return function() {
    // 删除数组第一位,并返回
    const obj = Array.prototype.shift.call(arguments);
    // 数组合并
    self.apply(obj, arguments)
  }
}

var yideng = Array.prototype.push.uncurring();
(function () {
  yideng(arguments, 4);
  console.log(arguments); // [1,2,3,4]
})(1, 2, 3);