jquery - scope inside $(document).ready()?

前端 未结 2 1198
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 01:51

So to stay organized, I have several javascript files, even though they all (in the end) are minified together to form one final javascript file.

Each file\'s conten

2条回答
  •  臣服心动
    2020-12-03 02:42

    It is a scope issue. For example:

    function a() {
       var myHiddenStr = 'abc';
    }
    alert(typeof(myHiddenStr));
    

    You cannot access myHiddenStr outside of function a. In similar fashion, the anonymous function you use for document ready hides everything within it.

    Having a global scope where you put things from different js files is not a good idea. It's probably better to have one document.ready handler and call respective functions from within it. You can then get the results out of the functions and pass them to other functions that need to use these.

提交回复
热议问题