Js函数参数能不能引用传递

wewewe 发布于 2016/05/16 14:49
阅读 112
收藏 0
JS


var hotel_index = 0;
        var dining_index = 0;
        var rides_index = 0;
        var get_there_index = 0;
        $('#hotel_right_arrow').click(function(){
        	if (hotel_index < len_hotels - 3){
            	hotel_index += 1;
            	display_or_not($(".hotel_box"), hotel_index);
            }
            if (hotel_index == len_hotels - 3){
            	$('#hotel_right_arrow').css({'opacity': '0.5'});
            }
            $('#hotel_left_arrow').css({'opacity': '1.0'});
        });
        $('#hotel_left_arrow').click(function(){
        	if (hotel_index > 0){
            	hotel_index -= 1;
            	display_or_not($(".hotel_box"), hotel_index);
            }
            if (hotel_index == 0){
            	$('#hotel_left_arrow').css({'opacity': '0.5'});
            }
            $('#hotel_right_arrow').css({'opacity': '1.0'});
        });
        //dining_right_arrow
        $('#dining_right_arrow').click(function(){
        	if (dining_index < len_dining - 3){
            	dining_index += 1;
            	display_or_not($(".dining_box"), dining_index);
            }
            if (dining_index == len_dining - 3){
            	$('#dining_right_arrow').css({'opacity': '0.5'});
            }
            $('#dining_left_arrow').css({'opacity': '1.0'});
        });
        $('#dining_left_arrow').click(function(){
        	if (dining_index > 0){
            	dining_index -= 1;
            	display_or_not($(".dining_box"), dining_index);
            }
            if (dining_index == 0){
            	$('#dining_left_arrow').css({'opacity': '0.5'});
            }
            $('#dining_right_arrow').css({'opacity': '1.0'});
        });
        //rides_right_arrow
        $('#rides_right_arrow').click(function(){
        	if (rides_index < len_rides- 3){
            	rides_index += 1;
            	display_or_not($(".rides_box"), rides_index);
            }
            if (rides_index == len_rides - 3){
            	$('#rides_right_arrow').css({'opacity': '0.5'});
            }
            $('#rides_left_arrow').css({'opacity': '1.0'});
        });
        $('#rides_left_arrow').click(function(){
        	if (rides_index > 0){
            	rides_index -= 1;
            	display_or_not($(".rides_box"), rides_index);
            }
            if (rides_index == 0){
            	$('#rides_left_arrow').css({'opacity': '0.5'});
            }
            $('#rides_right_arrow').css({'opacity': '1.0'});
        });
        //get there
        $('#get_there_right_arrow').click(function(){
        	if (get_there_index < len_get_there- 3){
            	get_there_index += 1;
            	display_or_not($(".get_there_box"), get_there_index);
            }
            if (get_there_index == len_get_there - 3){
                $('#get_there_right_arrow').css({'opacity': '0.5'});
            }
            $('#get_there_left_arrow').css({'opacity': '1.0'});
        });
        $('#get_there_left_arrow').click(function(){
        	if (get_there_index > 0){
            	get_there_index -= 1;
            	display_or_not($(".get_there_box"), get_there_index);
            }
            if (get_there_index == 0){
                $('#get_there_left_arrow').css({'opacity': '0.5'});
            }
            $('#get_there_right_arrow').css({'opacity': '1.0'});
        });


上面的代码怎么写成函数, 写成如下的形式啊,关键这个index变量怎么引用传递,我希望的效果是函数内的变化要影响到外面的各个index值

function right_arrow_clicked(a,b,c,d,index){.....}
$('#hotel_right_arrow').click(function(){
    right_arrow_clicked(a,b,c,d,index);
}
$('#dining_right_arrow').click(function(){
    right_arrow_clicked(a,b,c,d,index);
}
$('#rides_right_arrow').click(function(){
    right_arrow_clicked(a,b,c,d,index);
}






加载中
0
路G过
路G过
使用全局变量,在函数内部用this调用
0
Rezeroer
Rezeroer

基本类型不能引用传递,可以做成object就可以引用传递了

var indexobj = {index:0}

xxxxfn(indexobj)
xxxxfn(indexobj)

返回顶部
顶部