Shortcuts for jQuery's ready() method

后端 未结 3 1688
春和景丽
春和景丽 2020-12-12 20:45

I have seen some shortcuts for the ready() method and would like to know which actually happens first, because my test results confuse me..

$(document).ready         


        
3条回答
  •  无人及你
    2020-12-12 21:24

    Nick Craver is right in what he says but I think it is worth noting that in that last example that it isn't actually doing anything with jquery at all. jQuery is being passed as a parameter to the anonymous function but the function isn't doing anything with it.

    The last example is equivalent to an Immediately-Invoked Function Expression (IIFE):

    (function(){
        alert("self invoke");
    })();
    

    And clearly this is just immediately calling the anonymous function as soon as that line of code is being hit and thus doing the alert. It isn't invoking jQuery at all which is why Nick is right when he says it is defintiely not a ready() method.

提交回复
热议问题