簡體   English   中英

javascript函數作為參數和范圍變量

[英]javascript function as argument and scope variables

我有一個函數 - 讓我們將它定義為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多分鍾后叫,因為名字一樣-它是被叫AFTER ajax請求。

這只是一個例子 - 這個想法是這個after函數也是可變的,從完全不同的地方調用。


好的,得-1 ,因為我不知道我要求什么,所以我試着解釋這個問題。 我真的不明白變量是作為變量傳遞的,當它作為變量的指針傳遞時,可能會在它被傳遞給它的函數使用之前發生變化。

當我在一個函數中定義變量並將其傳遞給另一個函數時,它是指針還是獨立變量? 當我再次調用原點函數時,這些變量會改變 - 它會影響之前被“處理”的函數嗎? 或者變量之前的單詞var意味着 - 將它定義為完全新變量,並且每次調用帶有var的函數時,它都會將這個變量在內存中分配為全新的變量,不依賴於任何發生的變量?

我通常是一個PHP,C和Delphi程序員,我很難理解這些變量范圍是如何工作的,特別是當所有內容都是異步和回調時。

簡單地說,在javascript中,簡單類型(字符串,數字,布爾值)按值傳遞,而對象通過引用傳遞。 看到這個問題的真實詳細答案

如果您不知道要處理多少個參數(或它們的類型),那么您可以使用arguments數組

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);
});

因此,要解決您的問題,您必須通過將它們作為參數傳遞來指定您希望為回調提供哪些變量。 rvignaciobfavaretto已經告訴你如何做到這一點

勝利的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") );
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM