JavaScript 函数作为参数和范围变量

javascript function as argument and scope variables

本文关键字:范围 变量 参数 函数 JavaScript      更新时间:2023-09-26

我得到了一个函数 - 让我们将其定义为function getdata(after){},它将函数作为after参数。很自然。此函数是从其他某个函数运行的,该函数在其作用域中定义了一些变量:

$('.button').click(function(){
   var rel = $(this).attr('rel');
   var mooo = $(this).parent().offset().left;
   getdata(function(){alert(rel);console.log(moo)});
});

我如何正确传递这些变量,所以它真的与单击按钮时相同?它不能是全局变量,因为用户可以垃圾邮件点击按钮,并且它应该保持变量在点击期间完全相同,即使after函数将在 10 分钟后以随机顺序调用,因为顾名思义 - 它是在 ajax 请求之后调用的。

这只是一个例子 - 这个想法是这个after函数也是可变的,从完全不同的地方调用。


好的,-1了,因为不清楚我的要求是什么,所以我尝试解释这个问题。我真的不明白变量何时作为变量传递,以及何时作为变量指针传递,在它被传递给的函数使用之前可能会发生变化。

当我在一个函数中定义变量并将其传递给另一个函数时,它是指针还是独立变量?当我再次调用 origin 函数时,这些变量会发生变化 - 它会影响之前"脱落"的函数吗?或者在变量之前加上一个词 var 意味着 - 将其定义为全新的变量,并且每次调用带有 var 的函数时,它都会将内存中的这个变量分配为全新的变量,不受发生任何事情的影响?

通常是一个PHP,C和Delphi程序,我很难理解这些变量范围是如何工作的,特别是当一切都是异步和回调时。

简单地解释一下,在javascript中,简单的类型(字符串,数字,布尔值)通过值传递,而对象通过引用传递。 请参阅此问题以获取真正的详细答案。

如果您不知道要处理多少个参数(或其类型),则可以使用参数数组

var getdata = function(){
    //your function logic
    //arguments is an array with all the args
    //the last one is the "after" function
    var after = arguments[arguments.length-1];
    return after(arguments);
};
$('.button').click(function(){
   var rel = $(this).attr('rel');
   var mooo = $(this).parent().offset().left;
   getdata(rel, moo, /*add all the arguments you want here...*/, function(args){
       alert(args[0]);
       console.log(args[1])
   });
});

我不确定你在问什么,但这是 rvignacio 答案的变体,允许任意数量的参数getdataafter

var getdata = function(after){
    //your function logic
    return after.apply(null, [].slice.call(arguments, 1));
};
$('.button').click(function(){
   var rel = $(this).attr('rel');
   var mooo = $(this).parent().offset().left;
   getdata(function(rel_param, moo_param){
       console.log(rel_param);
       console.log(moo_param)
   }, rel, moo);
});

你不能完全按照你的描述去做。您正在尝试做的是将关联的闭包传递给回调。在 JavaScript 中,使用变量的词法范围。这意味着

function outer (callback) {
  var a = 1, b = 2, c = 3;
  var inner = function () {
     // This works because inner is defined inside of outer
     // and has access to a,b,c
     console.log(a,b,c); 
  }      
  inner();
  callback();
}
outer(function() {
    // This won't work because it doesn't know where to get a,b,c from
    // Some languages may(could/allow?) this, JS doesn't
    console.log(a,b,c);
});

因此,要解决您的问题,您必须通过将变量作为参数传递来指定您希望哪些变量可用于回调。 Rvignacio和Bfavaretto已经向你展示了如何做到这一点

eval() 为赢(为输,真的)

好的,有一种方法,但它需要eval,所以你不应该使用它。无论如何,这里是为了好玩。 eval()确实在同一范围内执行,因此通过在词法范围内添加 eval 并调用它的回调,您可以实现您想要的。再说一次,我并不是建议你这样做。它破坏了封装,回调不应该知道其调用方的私有。但只是为了好玩.... http://jsfiddle.net/zbwd7/1/

/**
 * The callback will accept a single argument, it's a function that takes the name
 * of the variable you want to use and returns its value. DON'T EVER DO THIS IN REAL CODE
 * @callback {function(string): *} getVar The function that gives you access to the
 * caller's scope
 * @callback.varName {string} The name of the variable to retrieve
 * @callback.return {*} The value of the variable
 */
function outer (callback) {
  var a = 1, b = 2, c = 3;
  function getVar(varName) {
      return eval(varName);
  }      
  callback(getVar);
}

outer(function(getVar) {
    // outputs one, two, three
    console.log(getVar("a"), getVar("b"), getVar("c") );
});