0

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($)
asked Feb 23, 2016 at 2:34
11
  • Both are being executed on the document 'ready' event but the second one is being passed $ (usually jQuery) as an argument. Commented Feb 23, 2016 at 2:36
  • Be interested to see the closure of those. Just looks like your passing a variable in to an anonymous function on the second one. Commented Feb 23, 2016 at 2:37
  • $(document).ready(function() { $("#nav_lat").slicknav({prependTo:"#mobile_menu"}); }); $(function($) { $("#nav_lat").slicknav({prependTo:"#mobile_menu"}); }); Commented Feb 23, 2016 at 2:38
  • 1
    I find it somewhat unlikely that such a difference would cause non-operation on specific iphone models. Are you sure that there aren't other differences at play here? Commented Feb 23, 2016 at 2:38
  • yes! have been testing this for 3 days. I think something is happening to the namespace Commented Feb 23, 2016 at 2:39

1 Answer 1

1

$(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

answered Feb 23, 2016 at 2:39
Sign up to request clarification or add additional context in comments.

2 Comments

everyone says this but it does not work on a Iphone 6 but does work on a Iphone 6S
most likely it is something else, show your full code that demonstrate that behavior.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.