1

I seen a code snippet somewhere look like this:

$(document).ready(function(){
    (function($){ $.fn.disableSelection = function() {
        return this.attr('unselectable', 'on')
                   .css('user-select', 'none')
                   .on('selectstart', false); }; })(jQuery);

})

As you know, this

$(document).ready(function(){

and this

(function($){

are identical. So why should a programmer does so? Isn't (function($){ redundant in code above?

stack
  • 10,280
  • 19
  • 65
  • 117
  • 5
    `$(function() { ... })` is not the same as `(function($) { ... })(jQuery)`. – dfsq Apr 23 '17 at 09:44
  • http://stackoverflow.com/questions/24930807/jquery-best-practice-using-document-ready-inside-an-iife, http://stackoverflow.com/questions/2662778/what-is-the-difference-between-these-jquery-ready-functions – Bergi Apr 23 '17 at 09:46
  • 2
    The inner one is just a plain IIFE, not a ready wrapper. – Bergi Apr 23 '17 at 09:48
  • @Bergi thank you .. my answer is exactly the link you provided. – stack Apr 23 '17 at 10:03
  • Downvoter please leave a comment and explain what's wrong with my question? – stack Apr 23 '17 at 10:04
  • @Bergi One question else: I have a function like this `function test(){ /*do stuff*/ );}`. Now I can call it like either `test();` or `$(test());`. They seem identical too me. Both approaches call that function. Is there any different between them? – stack Apr 23 '17 at 10:21
  • 1
    `$(test())` will call jQuery with the return value of `test()` - most likely `undefined`, which is why it doesn't do anything. You probably meant `$(test)`, where jQuery installs the function as a dom ready handler - quite different from `test()`. – Bergi Apr 23 '17 at 11:52

1 Answers1

4

As you know, this

$(document).ready(function(){

and this

(function($){

No, they are not identical. They have different purpose.

The first gets handler when the html document is ready.

(function(){...})(); will be executed as soon as it is encountered in the script.

The second is self executing function. That doesn't wait for document ready.

Isn't (function($){ redundant in code above?

And I agree that the (function($){ is redundant. There is no need of that.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Aren't they? really? But I remember I've read somewhere `.ready()` is outdated and we have to use something like `(function($){` instead of that. Do you know what been that thing? – stack Apr 23 '17 at 09:36
  • @stack they aren't say. You misunderstood it wrong or the article is wrong. – Suresh Atta Apr 23 '17 at 09:40
  • @ANS the documentation says they are equivalent. – Junius L Apr 23 '17 at 09:41
  • 2
    @julekgwa Which documentation says that what is equivalent? The two lines the question asks about are definitely not identical. – Bergi Apr 23 '17 at 09:47
  • @Bergi this answer has been edited I was pointing out the `$(document).ready(function(){` and `(function($){` are equivalent. – Junius L Apr 23 '17 at 09:49
  • @julekgwa Ok we have a confusion here. There are two things. If you look at the code you wrote here in your comment (`$(document).ready(function(){`). `ready` function used the `self invoked` function inside of it. That doesn't mean `ready` and `self invoked` are equal – Suresh Atta Apr 23 '17 at 09:51
  • @julekgwa Thanks. But no, [they are not equivalent](http://stackoverflow.com/questions/3259496/jquery-document-ready-vs-self-calling-anonymous-function). – Bergi Apr 23 '17 at 09:57
  • @Bergi i was talking about `$(document).ready(function(){` and `$(function() {` from the documentation it says they are equivalent. – Junius L Apr 23 '17 at 10:04