functions inside or outside jquery document ready

前端 未结 5 1192
迷失自我
迷失自我 2020-11-28 04:17

Up until now I just put all my jQuery goodness inside the $(document).ready() function, including simple functions used in certain user interactions.

B

5条回答
  •  醉梦人生
    2020-11-28 04:56

    Put it inside so it won't pollute the global namespace. It also ensures a faster resolve of the function name because of JavaScript's scope chains.

    Put it outside if it's a reusable component so you could easily move it in a separate file and call from different contexts.

    Since you already use JQuery, it's worth mentioning, that in your case you may want to define hexvalidate as a JQuery plugin outside and then invoke it inside.

提交回复
热议问题