面試總結 - 基礎編程 - 實現bind4.js

實現一個bind

Function.prototype.bind1 = function (context) {
  const args = Array.prototype.slice.call(arguments, 1);
  const self = this;
  console.log(this, args, arguments)
  return function () {
    const innerArgs = Array.prototype.slice.call(arguments);
    const finalArgs = args.concat(innerArgs);
    return self.apply(context, finalArgs);
  }
}

// test
const a = { 's': 3 }
function co (x) {
  console.log(x, this)
}
co.bind1(a, '----')()

唯一不變的,恐怕只有變化本身了吧~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章