1

I have a doubt with the syntax in the Twitter Bootstrap's JS files. This is an example file (bootstrap-dropdown.js):

!function ($) {

/*  ...    */

  /* APPLY TO STANDARD DROPDOWN ELEMENTS -- QUESTION CODE
   * =================================== */
  $(function () {
    $('html').on('click.dropdown.data-api', clearMenus)
    $('body')
      .on('click.dropdown', '.dropdown form', function (e) { e.stopPropagation() })
      .on('click.dropdown.data-api', toggle, Dropdown.prototype.toggle)
  })

}(window.jQuery);

Why they have a code block inside the jQuery function $(function(){}) ? I think it works without the jQuery function enclosing.

Thanks

2 Answers 2

1

This function behaves just like $(document).ready(), in that it should be used to wrap other $() operations on your page that depend on the DOM being ready.

Check the doc.

Sign up to request clarification or add additional context in comments.

2 Comments

so, if I declared this JS FiLE at the end of the BODY, the jQuery function enclosing doesn't have any effect?
@JoseS almost no effect, there may be things that the browser do between the end of </body> and the DOM being ready. You should keep it, at least as a security.
1
!function ($) {
    ...
}(window.jQuery);

That is used to create a scope (think namespace) who's variables will not pollute the global namespace.

If that would not be used, the toggle variable for example, would pollute the global namespace.

1 Comment

thanks for the answer but I'm asking about the inner jQuery function enclosing, after the comment that says "APPLY TO STANDARD DROPDOWN ELEMENTS -- QUESTION CODE"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.