0

I am trying to understand how a js library works, more specifically http://toopay.github.io/bootstrap-markdown/.

The whole js file is wrapped in this function:

!function ($) {
}(window.jQuery);

What does this do and how would I be able to call it again from outside the library?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vlad Otrocol
  • 2,952
  • 7
  • 33
  • 55
  • 4
    This is immediately invoked function, similar to `(function($) {})(jQuery)`. – dfsq Nov 13 '14 at 11:26
  • how can I reinvok this function from outside the library when I need it? – Vlad Otrocol Nov 13 '14 at 11:27
  • You can't and you should not. It's used only to create a function scope to protect the value of `$` and make sure that inside this function `$` is always points to `jQuery`. – dfsq Nov 13 '14 at 11:28
  • Why do you think you need to reinvoke it? – JJJ Nov 13 '14 at 11:29
  • my real problem is this: http://stackoverflow.com/questions/26905685/rails-stange-behaviour-when-using-bootstrap-markdown-gem @Juhana – Vlad Otrocol Nov 13 '14 at 11:32
  • I thought that if I can recall this initialisation function manually on my page I would be able to generate the element I need. @Juhana – Vlad Otrocol Nov 13 '14 at 11:33

1 Answers1

1

It creates a function then immediately calls it. The reason it does that is just so all the variables it makes won't be global, as they'll be inside the function instead. The ! is just to avoid causing problems when it's concatenated with other scripts and to save space.

You can't call it yourself, nor should you.

Community
  • 1
  • 1
Andrea
  • 19,134
  • 4
  • 43
  • 65