How to bind function arguments without binding this?

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

    Why not use a wrapper around the function to save this as mythis ?

    function mythis() {
      this.name = "mythis";
      mythis = this;
      function c(a, b) {
        this.name = "original";
        alert('a=' + a + ' b =' + b + 'this = ' + this.name + ' mythis = ' + mythis.name);
        return "ok";
      }    
      return {
        c: c
      }
    };
    
    var retval = mythis().c(0, 1);
    

提交回复
热议问题