从函数调用它使用回调参数 [英] Use parameter in callbacks from function which called it

查看:129
本文介绍了从函数调用它使用回调参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我调用一个函数的传递参数​​参数,其中一个异步函数B被调用,将异步函数B的回调C下能够使用给一个函数参数参数?如果是,将这种变化如果在函数B开始和回调C之间的时间被调用我重新调用函数的?

if i call a function A passing a parameter param, in which an asynchronous function B is called, will the callback C of the asynchronous function B be able to use the parameter param given to function A ? if yes, will this change if in the time between the function B start and the callback C is invoked i re-invoke function A?

例如:

function A(param) {

  value1 = param;
  doc = "hello";
  //this is the async function B;
  database.insert(doc, function() {
    //this is the invoked callback C when the async function is solved. 

    console.log(value1)

    //can i log value1? yes. if yes, will this change if i re-invoke 
    //function A before the callback is invoked or two different processes will start?

  })


}


A('hello');
A('not hello');

想知道如果这,如果第二次的函数为previous调用回调函数调用之前,将打印在控制台上正确的价值观:

Wondering if this, if for the second time A function is invoked before the callback function of the previous invocation, will print the right values on console:

您好;
不打招呼;

hello; not hello;

和永不
不打招呼;
不打招呼;

and never not hello; not hello;

原因第二次的调用感染首次之一。

cause second time's invocation infects first time one.

推荐答案

是,内联回调传递给 database.insert()将能够看到传递的参数以功能 A()。一个函数可以访问任何参数,甚至局部变量中包含的功能。

Yes, the inline callback passed to database.insert() will be able to see the argument passed to function A(). A function has access to any arguments or even local variables in the containing functions.

和,每个人都会被单独保存,所以你可以叫 A()多次,只要你想,他们将各自保留单独的参数。

And, each one will be saved separately so you can call A() as many times as you want and they will each retain separate arguments.

此概念被称为闭包,为()的每次调用一个创建一个独立的封闭和截流保持活着,只要它里面所有code (包括异步回调)尚未完成

This concept is called a closure, a separate closure is created for each invocation of A() and the closure stays alive as long as any code inside it (including asynchronous callbacks) has not yet completed.

您虽然需要在他们面前用 VAR 来声明你的局部变量他们是真正的局部变量和的每次调用独特A()。如果没有 VAR ,他们变得​​含蓄的全局变量和的一个调用A()会搞乱与另一个全局变量 A的调用()。如果你不喜欢这样,你是安全的:

You WILL though need to declare your local variables with var in front of them for them to be truly local variables and unique for each invocation of A(). Without the var, they become implicit global variables and one invocation of A() will mess with the global variables of another invocation of A(). If you do it like this, you are safe:

function A(param) {

  var value1 = param;
  var doc = "hello";
  //this is the async function B;
  database.insert(doc, function() {
    //this is the invoked callback C when the async function is solved. 

    console.log(value1);
    console.log(param);
    console.log(doc);

    //can i log value1? yes. if yes, will this change if i re-invoke 
    //function A before the callback is invoked or two different processes will start?

  })


}


A('hello');
A('not hello');

这篇关于从函数调用它使用回调参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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