jQuery变量范围超过函数调用?

时间:2011-11-11 19:52:17

标签: javascript jquery scope

我有这个功能

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

我的问题是如何保留在update_prices函数中创建的三个变量standard_selector business_selectorprofessional_selector的范围

2 个答案:

答案 0 :(得分:5)

为了在函数声明之后保持这些变量的持久性,你有以下选择:

  1. 必须在较高级别范围内声明它们,并在您需要的时间内持续存在。
  2. 他们需要是持续网页生命周期的全局变量。
  3. 需要将它们指定为某个对象的属性,该对象持续所需的持续时间。这可以通过从函数返回具有这些值的对象或将对象传递到可以设置属性的函数中,或者通过具有放置属性的全局对象来完成。
  4. 最简单的解决方案(并不总是最好的)是通过在更高的范围内声明它们并在函数中删除它们前面的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
    

答案 1 :(得分: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");