How to bind function arguments without binding this?

前端 未结 15 952
爱一瞬间的悲伤
爱一瞬间的悲伤 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:50

    May be you want to bind reference of this in last but your code:-

    var c = function(a, b, c, callback) {};
    var b = c.bind(null, 1, 2, 3); 
    

    Already applied binding for instance this and later you can not change it. What I will suggest that use reference also as a parameter like this:-

    var c = function(a, b, c, callback, ref) {  
        var self = this ? this : ref; 
        // Now you can use self just like this in your code 
    };
    var b = c.bind(null, 1, 2, 3),
        newRef = this, // or ref whatever you want to apply inside function c()
        d = c.bind(callback, newRef);
    

提交回复
热议问题