Why do I see MS jQuery samples use `$(domReady); `?

和自甴很熟 提交于 2019-12-10 17:35:57

问题


I don't know if anyone ELSE has noticed this, but I noticed the jQuery samples I see on MS tend to use a different format:

<script type="text/javascript">  

  $( domReady );  

  function domReady() {  
     $('#btn').click( showMessage );  
  }  

  function showMessage() {  
     $('#message').fadeIn('slow');  
  }  
</script> 

Isn't this the same as:

$(document).ready( function() {

  $('#btn').click( showMessage );  

  function showMessage() {  
     $('#message').fadeIn('slow');  
  }  

});

Is there any advantage of using one syntax over the other?

I will admit, the MS way does look cleaner.


回答1:


The only real difference is readability and organization.

It's technically the same as

$( function() {

  $('#btn').click( showMessage );  

  function showMessage() {  
     $('#message').fadeIn('slow');  
  }  

});

which is shorthand for $(document).ready(...)



来源:https://stackoverflow.com/questions/2625220/why-do-i-see-ms-jquery-samples-use-domready

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!