将功能输入其他功能

时间:2020-04-02 18:24:26

标签: javascript function

我正在尝试将一个功能输入另一个功能,然后运行它。

我有一个功能。

function example_function() {
    document.print('example')

,我想输入另一个函数以使其执行 N 次。

function do_n_times(function, times) {
    for (var i = times; i < times; i++) {
        do(function)
    }
}

有没有办法做到这一点,如果可以的话。您还可以进行自定义输入吗?

function do_n_times_with_this_input(function, times, input) {
    for (var i = times; i < times; i++) {
        do(function(input))
    }
}

2 个答案:

答案 0 :(得分:1)

您绝对可以做到这一点! JavaScript具有所谓的一流函数,这意味着它们可以像其他任何变量一样传递。语法如下:

function example_function() {
  console.log('example')
}

function do_n_times(func, times) {
  for (var i = 0; i < times; i++) {
    func();
  }
}

do_n_times(example_function, 5);

答案 1 :(得分:0)

是的,您可以按照自己的描述去做。在JavaScript中,可以通过在末尾附加()来将任何变量作为函数调用:

function f1() {
  console.log("f1 called")
}

const f2 = () => console.log("f2 called");

const nonFn = 42;

f1();
f2();

try {
  nonFn();
} catch (e) {
  console.log("Error because nonFn is not a function")
}

您可以按照需要的任何方式进一步传递函数:

function f1() {
  console.log("f1 called")
}

const f2 = () => console.log("f2 called");

function caller(fn) {
  fn();
}

caller(f1);
caller(f2);
caller(() => console.log("directly defined arrow function called"));
caller(function() { console.log("directly defined normal function called"); });

这也意味着您也可以传递任何想要的输入:

function f1(input) {
  console.log("f1 called with", input)
}

const f2 = input => console.log("f2 called with", input);

function caller(fn, input) {
  fn(input);
}

caller(f1, "foo");
caller(f2, "bar");
caller(
  input => console.log("directly defined arrow function called with", input),
  "baz"
);
caller(
  function(input) { console.log("directly defined normal function called with", input); },
  "quux"
);

这是基本内容,您可以使用Function#callFunction#apply来控制如何执行函数。在两种情况下,您都可以执行函数和change its context

function f() {
  console.log("f called with context:", this);
}

function caller(fn) {
  const newContext = {foo: "hello"}
  fn.call(newContext);
}

function applier(fn) {
  const newContext = {bar: "world"}
  fn.apply(newContext);
}

caller(f);
applier(f);

在两种情况下,您都可以传递参数。 The difference between .call() and .apply()是您通过它们的形式。而且,如果您不关心上下文(该函数未使用this),则可以仅使用null作为其参数:

function f(a, b, c) {
  console.log(
    "f called with arguments",
      "\n\ta:", a,
      "\n\tb:", b,
      "\n\tc:", c
  );
}

function caller(fn, a, b, c) {
  fn.call(null, a, b, c);
}

function applier(fn, a, b, c) {
  fn.apply(null, [a, b, c]);
}

caller(f, "passed", "directly", "into");
applier(f, "passed", "as", "array");

差异似乎可以忽略不计,但这是.apply的一个很好的用例-您可以为函数提供无限的参数:

function f(...args) {
  console.log(
    "f called with arguments:", args
  );
}

function applier(fn) {
  const argumentsForFunction = Array.prototype.slice.call(arguments, 1);
  fn.apply(null, argumentsForFunction);
}

applier(f, "one");
applier(f, "alpha", "beta", "gamma");
applier(f, "a", "b", "c", "d", "e", "f", "g");

请注意如何使用Array#slice调用the arguments object,以便排除第一项(函数)并占用其余项。

您可以完全控制函数的执行方式。使用这些。

最后一个选项是Function#bind-它的工作原理与call类似,因为您可以更改上下文并传递参数,但不同之处在于它返回了 new函数将始终使用这些。这具有有用的应用程序-您可以永久设置this so you never "loose" it

正在创建.bind()方法的另一个有用的应用程序,称为“部分应用的函数”-这是一个通常具有多个参数的函数,但是您已经永久设置了其中一些参数,因此该功能仅等待其余的或什至只是执行:

function f(a, b) {
  console.log(
    "f called with arguments",
      "\n\ta:", a,
      "\n\tb:", b
  );
}
//null for the context - we don't care for it here
const f1 = f.bind(null, "hello");

f1("world");
f1("my baby");
f1("my honey");

const f2 = f1.bind(null, "my ragtime gal");

f2();