How to bind function arguments without binding this?

前端 未结 15 930
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 07:25

In Javascript, how can I bind arguments to a function without binding the this parameter?

For example:

//Example function.
var c = funct         


        
15条回答
  •  日久生厌
    2020-11-30 07:42

    Simple like that?

    var b = (cb) => obj.c(1,2,3, cb)
    b(function(){}) // insidde object
    

    More general solution:

    function original(a, b, c) { console.log(a, b, c) }
    let tied = (...args) => original(1, 2, ...args)
    
    original(1,2,3) // 1 2 3
    tied(5,6,7) // 1 2 5

提交回复
热议问题