最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - difference between document.ready() inner function and a function in <script><script> t

programmeradmin1浏览0评论

What is the difference between these two functions?

1:

$(document).ready(function myfunc() {
   function dosomething() {
      // do something
   }
});

2:

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

What is the difference between these two functions?

1:

$(document).ready(function myfunc() {
   function dosomething() {
      // do something
   }
});

2:

<script language="javascript">
function dosomething() {
   // do something
}
</script>
Share Improve this question edited Apr 25, 2010 at 0:48 Bill the Lizard 406k212 gold badges574 silver badges892 bronze badges asked Apr 17, 2010 at 8:30 DELETE meDELETE me
Add a ment  | 

2 Answers 2

Reset to default 9

The $(document).ready() function executes when the DOM has finished loading. See http://api.jquery./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 plete 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)

  1. 暂无评论