0

This is my code:

jQuery

(function($) {
    $(window).load(function(){
        $(".bottomWrapperTable").mCustomScrollbar({
            axis: "y",
            theme: "dark",
            scrollbarPosition: "outside",
            callbacks: {
                whileScrolling: function(){
                    setScroll(this.mcs.left);
                },
                onScroll: function() {
                    setStartEndScroll(this.mcs.leftPct);
                }
            }
        });
    });
})(jQuery);

I followed the mCustomScrollar documentation to produce this code.

My question is, why does this function have to be within the immediate funtion even though window.load() is not called until after the DOM is ready anyway?

I have tried removing the immediate function but appears to not call the function at all. I also tried putting the code within $(function() {} but still no luck.

NOTE: I am not encountering a problem, just requesting information on this topic.

Any insight into this would be helpful

ozil
  • 6,930
  • 9
  • 33
  • 56
wmash
  • 4,032
  • 3
  • 31
  • 69
  • 1
    Its a practice called 'iffy'. It means you do not create any implicit globals. See: https://en.wikipedia.org/wiki/Immediately-invoked_function_expression – ste2425 Nov 13 '15 at 11:54
  • you can remove it and replace all `$` signs with `jQuery` – Danil Gudz Nov 13 '15 at 11:54
  • @GudzDaniel can you explain a bit more? Are you saying I can remove the immediate function and replace, for example, `$(window).load()` with `jQuery(window).load()`? – wmash Nov 13 '15 at 11:58
  • 1
    yes, iife is used for 1) local scope(not to create global variables), 2) rename variables like as you can see in your code iife invokes with the `jQuery` param and the function get that `jQuery` but renames it to `$` locally – Danil Gudz Nov 13 '15 at 12:00
  • Thank you to both of you! Helped clear up some confusion – wmash Nov 13 '15 at 12:02

0 Answers0