2011-11-11 42 views
1

我有這個功能通過函數調用jQuery變量作用域?

function update_prices(product_selector){ 
    //kind of a hack to account for the sometimes having a professional price and sometimes not 
    var price_count = product_selector.find('small.rt').length; 
    for (i=0;i<=price_count;i++) 
    { 
     if(i == 0){ 
      var standard_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var standard_price = standard_selector.attr('data'); 
     } 
     if(i == 1){ 
      var business_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var business_price = business_selector.attr('data'); 
     } 
     if(i == 2){ 
      var professional_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var professional_price = professional_selector.attr('data'); 
     } 
    } 
} 

,我有這個塊的代碼調用它

.... 
.... 
product_selector.find(".active_selector").removeClass('active_selector'); 
update_prices(product_selector); 
.... 
.... 

standard_selector.text("something"); 
business_selector.text("something else"); 
professional_selector.text("another thing"); 

我的問題是如何保持的範圍爲三個變量standard_selectorbusiness_selectorprofessional_selector是獲得創建在update_prices函數中

+0

是否希望能夠訪問update_prices()函數之外的那些變量? – maxedison

+1

您將它們作爲數組或對象返回,然後從函數調用中檢索它們。 – Blazemonger

+0

是直接在函數調用後 – Trace

回答

5

爲了讓這些變量在函數聲明後保持不變,您可以選擇這些選項:

  1. 它們必須在更高級別的範圍內聲明,並在您需要的時間內持續存在。
  2. 它們需要是全局變量,可以保存網頁的整個生命週期。
  3. 需要將它們分配爲持續所需持續時間的某個對象的屬性。這可以通過從函數返回一個包含這些值的對象,或者將一個對象傳遞給可以設置屬性的函數,或者通過使用一個全局對象來放置這些屬性來完成。

最簡單的解決方案(認爲並不總是最好的),是在較高的範圍,宣佈他們,在他們面前的功能刪除了var所以你只是在全局變量操作,而不是讓他們的全局變量

// declare global variables in global scope 
var standard_selector; 
var business_selector; 
var profession_selector; 

function update_prices(product_selector){ 
    //kind of a hack to account for the sometimes having a professional price and sometimes not 
    var price_count = product_selector.find('small.rt').length; 
    for (i=0;i<=price_count;i++) 
    { 
     if(i == 0){ 
      standard_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var standard_price = standard_selector.attr('data'); 
     } 
     if(i == 1){ 
      business_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var business_price = business_selector.attr('data'); 
     } 
     if(i == 2){ 
      professional_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var professional_price = professional_selector.attr('data'); 
     } 
    } 
} 

或者,如果你只是想從這個函數返回他們這樣你就可以在範圍內使用他們一個級別,那麼你可以在一個對象返回他們:

function update_prices(product_selector){ 
    //kind of a hack to account for the sometimes having a professional price and sometimes not 
    var sel = {}; 
    var price_count = product_selector.find('small.rt').length; 
    for (i=0;i<=price_count;i++) 
    { 
     if(i == 0){ 
      sel.standard_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var standard_price = standard_selector.attr('data'); 
     } 
     if(i == 1){ 
      sel.business_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var business_price = business_selector.attr('data'); 
     } 
     if(i == 2){ 
      sel.professional_selector = product_selector.find('small.rt:eq('+ i +')'); 
      var professional_price = professional_selector.attr('data'); 
     } 
    } 
    return(sel); 
} 

var selectors = update_prices(xxx); 
// access selectors.standard_selector, selectors.business_selector, selectors.profession_selector here 
+0

...但是根據我的說法,沒有任何理由將這些變量存儲在全局中。 – Blazemonger

+1

在這個問題中,關於這些變量是全局需要還是僅需要一個範圍的上下文信息都沒有。我試圖回答如何保留這些變量的問題。這就是爲什麼我提供了涵蓋所有基礎的三個選項,並且OP可以選擇最適合其場景的選項。 – jfriend00

+0

考慮到問題的本質,我認爲我們可以安全地推斷出OP不是經驗豐富的JS開發人員,只是不知道更好。 ;-) – Blazemonger

4
使用局部變量

將它們作爲對象返回

function update_prices(product_selector){ 
    ... 
    return {standard_selector:standard_selector, business_selector:business_selector, professional_selector} 
} 

var r = update_prices(product_selector); 
r.standard_selector.text("something"); 
r.business_selector.text("something else"); 
r.professional_selector.text("another thing");