search

Home  >  Q&A  >  body text

javascript - How to modify the return value of js function?

Now we need to encapsulate the pop-up layer into a component. The pop-up layer component contains two operation functions: "cancel" and "confirm", and the return values ​​are false and true respectively. How is the return value of this component determined by the return values ​​of these two operations?

code show as below:

 function tips(){
    //调用函数时,显示弹出层
    $('.mask').show();
    
    //取消
    $('.cancel').on('click',function(){
        $('.mask').hide();    //隐藏弹出层
        return false;
    });
    
    //确定
    $('.confirm').on('click',function(){
        $('.mask').hide();    //隐藏弹出层
        return true;
    });
}

I want to call it like this:

if( tips() ){
    do_something_true...
}else{
    do_something_false...
}
Thanks
ringa_leeringa_lee2748 days ago626

reply all(5)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:13:24

    function tips(cb){
        //调用函数时,显示弹出层
        $('.mask').show();
        
        //取消
        $('.cancel').on('click',function(){
            $('.mask').hide();    //隐藏弹出层
            cb(false);
        });
        
        //确定
        $('.confirm').on('click',function(){
            $('.mask').hide();    //隐藏弹出层
            cb(true);
        });
    }
    
    tips(function(ret){
      if(ret) {
         // blabla
      } else {
        // blabal
      }
    });

    If you want to show off your skills, you can do it in ES7:

    function tips(){
      return new Promise(resolve => {
        //调用函数时,显示弹出层
        $('.mask').show();
        
        //取消
        $('.cancel').on('click',function(){
            $('.mask').hide();    //隐藏弹出层
            resolve(false);
        });
        
        //确定
        $('.confirm').on('click',function(){
            $('.mask').hide();    //隐藏弹出层
            resolve(true);
        });
      });
    }
    
    async function test() {
      if( await tips()) {
        // do_something_when_true...
      } else {
        // do_something_when_false
      }
      // 是不是熟悉的味道?Promsie是个好东西,async/await更是
    }
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:13:24

    function tips(confirmCallback, cancelCallback){
        //调用函数时,显示弹出层
        $('.mask').show();
        
        //取消
        $('.cancel').on('click',function(){
            $('.mask').hide();    //隐藏弹出层
            cancelCallback && cancelCallback();
            return false;
        });
        
        //确定
        $('.confirm').on('click',function(){
            $('.mask').hide();    //隐藏弹出层
            confirmCallback && confirmCallback();
            return true;
        });
    }

    reply
    0
  • ringa_lee

    ringa_lee2017-05-19 10:13:24

    Add a flag. Flag can also be represented by numbers like 0 and 1.

    function tips(){
        var flag = false;
        //调用函数时,显示弹出层
        $('.mask').show();
        
        //取消
        $('.cancel').on('click',function(){
            $('.mask').hide();    //隐藏弹出层
            flag = false;
        });
        
        //确定
        $('.confirm').on('click',function(){
            $('.mask').hide();    //隐藏弹出层
            flag = true;
        });
        return flag;
    }

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-19 10:13:24

    I just learned a very stupid method
    var tips_vla="";
    function tips(){

    //调用函数时,显示弹出层
    $('.mask').show();
    //取消
    $('.cancel').on('click',function(){
        $('.mask').hide();    //隐藏弹出层
        tips_vla=0;
        al(tips_vla);
    })
    //确定
    $('.confirm').on('click',function(){
        $('.mask').hide();    //隐藏弹出层
        tips_vla=1;
        al(tips_vla);
    });

    }
    tips();
    function al(){
    if(tips_vla){

    alert("你点击了确认");

    }else{

      alert("你点击了取消");

    }
    }

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-19 10:13:24

    The laziest way is to add an exclamation mark, hee hee hee

    if( !tips() ){
        do_something_true...
    }else{
        do_something_false...
    }

    Of course, it is best to write a sentence in the tips() function, return true

    reply
    0
  • Cancelreply