javascript - How to modify the return value of js function?
ringa_lee
ringa_lee 2017-05-19 10:11:24
0
5
567
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_lee
ringa_lee

ringa_lee

reply all(5)
淡淡烟草味
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更是
}
大家讲道理
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;
    });
}
左手右手慢动作

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;
}
曾经蜡笔没有小新

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("你点击了取消");

}
}

滿天的星座

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!