Search code examples
javascriptzepto

why $ is passed as argument to callback($) in the Zepto library where they define DOMContentLoaded event listener?


I'm browsing through the Zepto source code and there is this little function here: gitHub - Zepto.js

ready: function(callback){
      if (readyRE.test(document.readyState)) callback($)
      else document.addEventListener('DOMContentLoaded', function(){ callback($) }, false)
      return this
    }

I don't understand why $ is passed as argument to the callback??


Solution

  • It's so that there's a convenient, local reference to the library that can be called whatever you want it to. So, for example, jQuery (which does the same thing) may not be called $, but you can call it that without an extra function by doing:

    jQuery(document).ready(function($) {
        // Your jQuery code here, which uses $ as an alias for jQuery
    });