TIL icon indicating copy to clipboard operation
TIL copied to clipboard

bind, 函数的第二个参数会输出什么?

Open xiaohesong opened this issue 6 years ago • 1 comments

今天看到一个地方有误解,自己试了下,感觉挺不错。

你觉得下面的这个consolesaga会输出什么?

function run({name, age}, saga, ...options){
  console.log('name is', name, 'saga is', saga)
}
let boundRun = run.bind(null, {name: 'xiaohesong', age: 18})

function *rootSaga() {
  yield 'i am a generator function'
}

boundRun(rootSaga)

xiaohesong avatar Dec 13 '18 09:12 xiaohesong

来实现个bind函数。

bind = (fn, _this, ...bindArgs) => (...args) => fn.apply(_this, [...bindArgs, ...args])

xiaohesong avatar Dec 28 '18 08:12 xiaohesong