What does “[removed]void(0)” mean?

前端 未结 14 1371
说谎
说谎 2020-11-21 15:23
login

I\'ve seen such hrefs many times, but I don\'t know what exactly

14条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 16:23

    javascript: URLs aside; this is where void can be used to write shorter code:

    var error1 = false,
        error2 = false,
        error3 = false;
    
    function error1() {
    
        error1 = true;
    }
    
    function myFunction() {
    
        // You can easily return and call a function at once, if you don't care about myFunction's return value
        if (!someCondition1)
            return error1();
    
        // What if you want to set a value first?
        if (!someCondition2) {
            error2 = true;
            return
        }
    
        // Shortest way to return and set a value at once
        if (!someCondition3)
            return void(error3 = true);
    
        // More code goes here
    }
    
    

提交回复
热议问题