JackNova
JackNova

Reputation: 3931

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??

Upvotes: 1

Views: 797

Answers (2)

Ry-
Ry-

Reputation: 225044

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
});

Upvotes: 3

Code Maverick
Code Maverick

Reputation: 20415

If you look at the bottom of the repository on gitHub, you'll see:

// If `$` is not yet defined, point it to `Zepto`
window.Zepto = Zepto
'$' in window || (window.$ = Zepto)

It's basically giving you a shorthand for Zepto.

Upvotes: 0

Related Questions