开发者

difference between document.ready() inner function and a function in <script></script> tag

What is the difference between these two functions?

1:

$(document).ready(function 开发者_StackOverflow社区myfunc() {
   function dosomething() {
      // do something
   }
});

2:

<script language="javascript">
function dosomething() {
   // do something
}
</script>


The $(document).ready() function executes when the DOM has finished loading. See http://api.jquery.com/ready/

Whereas the function is not executed until called. If you were to have a call to that function, it would happen as it is loading and not wait for any external event to complete as in the former. Like:

<script language="javascript">
dosomething();
function dosomething(){
// do something
}
</script>


In the first example, your inner function dosomething() will be limited to the scope of myfunc().

In the second case, the dosomething() function will be added to the global space. It will be accessible from anywhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜