stack
stack

Reputation: 10228

Is wrapping a js code into .ready() twice reasonable?

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?

Upvotes: 1

Views: 41

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 121998

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.

Upvotes: 4

Related Questions