DCR
DCR

Reputation: 15665

$(document).ready(function() versus $(document).ready(function($)

I've been working with Iphone 6 and 6S and found that the first way only work on the 6S while the second way works on both. (both use IOS 9.2.1) could someone explain the difference between:

$(document).ready(function()

and

$(document).ready(function($)

Upvotes: 0

Views: 47

Answers (1)

vittore
vittore

Reputation: 17579

$(document).ready(function() {
   console.assert(jQuery == arguments[0] , 'my first param is jquery object')
     console.assert(this == document , 'my this is document')
       
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

There is no difference except naming first parameter, try:

 $(document).ready(function($) {
     console.log(arguments)

and

 $(document).ready(function() {
   console.log(arguments)
 })

Javascript does not have method overload based on parameters.

UPDATE: regardless of what definition of anonymous (or named function for that sake) you are passing , jquery will call your function as:

  func.apply(document, jquery)

See source: https://github.com/jquery/jquery/blob/99e8ff1baa7ae341e94bb89c3e84570c7c3ad9ea/src/callbacks.js#L80

So your first argument, wether you name it $ or not going to be jquery object, and your this going to be document

Upvotes: 1

Related Questions