bind, apply, call

區別

bind更改函數的this並返回新函數, 能夠傳參app

call和apply都是改變上下文中的this並當即執行這個函數函數

封裝各類公共參數, 以及自定義參數的例子

var test = (() => {
  const funcTest = (...args) => {
    console.log('funcTest', [...args])
  }
  // apply  當即執行
  const test2 = (...args) => {
    console.log('test2', ...args)
    funcTest.apply(null, ['inner', ...args])
  }
  // 使用bind生成新函數
    return {
    test2,
    src: test2.bind(null, 'config.dir_src'),
    dist: test2.bind(null, 'config.dir_dist'),
    test: test2.bind(null, 'config.dir_test')
  }
})()

clipboard.png

相關文章
相關標籤/搜索