Javascript wrapping code inside anonymous function

前端 未结 5 1070
有刺的猬
有刺的猬 2020-12-13 02:40

I need help understanding this pattern for jQuery plugin authoring. Can somebody explain this simple bit of code for me?

(function($) { /* Code here */ })(jQ         


        
5条回答
  •  悲&欢浪女
    2020-12-13 03:19

    +1 for jfriend00's answer.

    But including jQuery in a page overwrites both of the global symbols jQuery and $ (see jQuery.js line 9579) potentially causing conflicts with other libraries that define a global $.

    So taking this a step further to also stop the global $ conflict:

    (function($) {
        // you can use $ here to refer to jQuery
    })(jQuery.noConflict());
    

    as demonstrated by:

    
    
    

提交回复
热议问题