调用.apply()后,如何保持原始函数的此上下文? [英] How do I keep the this context of my original function after calling .apply()?

查看:43
本文介绍了调用.apply()后,如何保持原始函数的此上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个functionA,它接受一个函数作为参数.我想在functionA中操纵该函数的参数,并将其作为functionC返回.我发现我可以使用.apply()做到这一点,但是functionB的原始上下文丢失了,而是由functionA代替了.

I have a functionA which accepts a function as a parameter. I want to manipulate the arguments of that function in functionA and return this as functionC. I found that I can do this with .apply(), however the original context of functionB is lost and instead replaced by functionA.

例如,

var factory = {
   return {
       fnB: function() {}  
   }
};

fnA(fn) {
    return fnC(params) {
        var customparams = [params, {something: else}]
        return method.apply(null, customparams);
     }
}

var load = fnA(factory.fnB);
load(params);

但是,当我执行load(params)时,我失去了functionB的上下文.FunctionB被定义为工厂提供的方法.我该怎么办?谢谢!

However when I execute load(params), I lose functionB's context. FunctionB is defined as a method from a factory. How can I go about this? Thanks!

推荐答案

我将假设方法实际上是 fn ,并且是 functionB 是分配给对象属性的函数,并且在您的实际代码中不存在各种语法错误:

I'm going to assume method is actually fn, and that functionB is a function assigned to an object property, and that the various syntax errors aren't present in your actual code:

var obj = {
    name: "foo",
    functionB: function() {
        console.log(this.name); // <== Using `this` to refer to `obj`
    }
};

function functionA(fn) {
    return function functionC(params) {
        var customparams = [params, {something: "else"}];
        return fn.apply(null, customparams);
    };
}

var load = functionA(obj.functionB);
load("a", "b"); // Fails because `this` in the call to `functionB` isn't `obj`

如果是这样,您可以在两次麻烦中解决它:

If so, you can fix it in a couple of wasy:

1)通过将 functionB 传递到 functionA 时使用 Function#bind :

1) By using Function#bind whne passing functionB into functionA:

var load = functionA(obj.functionB.bind(obj));
load("a", "b"); // Works

var obj = {
  name: "foo",
  functionB: function() {
    snippet.log(this.name); // <== Using `this` to refer to `obj`
  }
};

function functionA(fn) {
  return function functionC(params) {
    var customparams = [params, {
      something: "else"
    }];
    return fn.apply(null, customparams);
  };
}

var load = functionA(obj.functionB.bind(obj));
load("a", "b"); // Works

<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

2)通过在 functionA 中添加第二个参数,

2) By adding a second argument to functionA, as Bergi suggested, that it uses when calling fn (see in the snippet) — this approach is used by many of the ES5 Array methods, for instance:

var obj = {
  name: "foo",
  functionB: function() {
    snippet.log(this.name);
  }
};

function functionA(fn, thisArg) {           // <=== Accepting thisArg
  return function functionC(params) {
    var customparams = [params, {
      something: "else"
    }];
    return fn.apply(thisArg, customparams); // <=== Using it
  };
}

var load = functionA(obj.functionB, obj);   // <=== Passing it
load("a", "b"); // Works

<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

这篇关于调用.apply()后,如何保持原始函数的此上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆