我有这个功能
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_selector
和professional_selector
的范围
答案 0 :(得分:5)
为了在函数声明之后保持这些变量的持久性,你有以下选择:
最简单的解决方案(并不总是最好的)是通过在更高的范围内声明它们并在函数中删除它们前面的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");