简体   繁体   English

我似乎无法将参数传递给回调

[英]I can't seem to get my arguments to pass into a callback

I've created a working recursive for loop, but it only works if my callback has no arguments. 我创建了一个工作递归的for循环,但是只有在我的回调没有参数的情况下它才有效。 I've tried callback(arguments) and callback(...arguments) . 我已经尝试过callback(arguments)callback(...arguments)

Thanks for any help you can provide! 感谢您的任何帮助,您可以提供!

 function loopFunc (numOfSteps, callback) { let i = 0; if (i >= numOfSteps) { let i = 0 return } callback() loopFunc(numOfSteps - 1, callback)`enter code here` } 

It works if the callback takes no arguments: 如果回调不接受任何参数,它将起作用:

 function noArgsHello() { console.log('hello') } const thisWorks = loopFunc(3, noArgsHello); thisWorks() 

It doesn't work if the callback takes an argument: 如果回调函数接受一个参数,它将不起作用:

 function sayHello (input) { console.log(input) } const thisDoesntWork = loopFunc(3, sayHello('hello'); thisDoesntWork() 

You almost there! 你快到了! It depends what is your goal here, you can use either option: 这取决于您的目标是什么,您可以使用以下任何一种方法:

function loopFunc (numOfSteps, callback) {
let i = 0;

  if (i >= numOfSteps) {
      let i = 0
      return
  }

 callback(numOfSteps)
 loopFunc(numOfSteps - 1, callback);
}

function printExtraStuff(greeting) {
  return (val) => { console.log('greeting ', val)}
}

function printSteps(num) {
    console.log(num);
}

var test1 = function() { loopFunc(3, printExtraStuff('hi there') )};
test1()

var test2 = function() { loopFunc(3, printSteps )};
test2()

In this way: 通过这种方式:

const thisDoesntWork = loopFunc(3, sayHello('hello');

You are not passing a callback anymore, but you are executing the sayHello function and passing the returned value of that function to the loopFunc . 您不再传递回调,而是在执行sayHello函数并将该函数的返回值传递给loopFunc

What you can do in these cases, is to use the bind method of functions for passing the function with arguments: 在这些情况下,您可以做的是使用函数bind方法通过参数传递函数:

const thisDoesntWork = loopFunc(3, sayHello.bind(sayHello, 'hello'));

Or you can pass directly a function which executes your sayHello so that the argument is still a function and it will be used as callback inside your loopFunc : 或者,您可以直接传递一个执行sayHello的函数,以便该参数仍然是一个函数,并将其用作loopFunc内的loopFunc

const thisDoesntWork = loopFunc(3, () => sayHello('hello'));

You need to use an anonymous function if you want to pass parameters in an argument -based function ( callback is an argument). 如果要在基于参数的函数中传递参数( callback是一个参数),则需要使用匿名函数。 Your code should instead be like this: 您的代码应改为:

 function sayHello(input) { console.log(input) } const works = () => sayHello('hello'); works(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我似乎无法做出反应将这些方法传递给我的组件 - I can't seem to get react to pass down these methods to my component 我似乎无法使我的代码正常工作 - I can't seem to get my code to work 我似乎无法让我的Javascript表单验证正常工作 - I can't seem to get my Javascript form validation to work 我似乎无法让我的 React 私人路线工作 - I can't seem to get my React private route to work 我似乎无法使我的下拉代码正常运行 - I can't seem to get my dropdown code to function properly 使用 Ajax 时,我仍然无法让我的回调工作。 它似乎没有在等待回调,我做错了什么? - I still can't get my callbacks to work when using Ajax. It doesn't seem to be waiting for the callback, what am I doing wrong? 在 Firebase 的实时数据库中,如何将自己的 arguments 传递给事件侦听器的回调? - In Firebase's Realtime Database, how can I pass my own arguments to an event listener's callback? 如何将带有多个参数的函数引用传递给jQuery(document).on('click'事件的回调? - How can I pass a function reference with multiple arguments to the callback of my jQuery(document).on('click' event? 似乎无法让我的jQuery工作 - Can't seem to get my jQuery to work 我似乎无法让我的代码显示我的计算,我是否缺少 output 标签? - I can't seem to get my code to display my calculation, am I missing an output tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM