jQuery dollar sign ($) as function argument?

后端 未结 4 1223
梦毁少年i
梦毁少年i 2020-11-27 13:09

I understand JavaScript closures, and I\'ve seen this done in native JS:

(function () {
  // all JS code here
})();

But, what does adding t

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 14:01

    Its a way of mapping jQuery to the $ in a way so that not all code in a page will see it.

    Maybe you have an existing script that uses jQuery that you like to reuse but you also use prototype that also uses $ in the same page.

    By wrapping any jQuery using code in that construct you redefine $ to jQuery for the contained part without coming into conflict with other code in the page.

提交回复
热议问题