Reputation: 10228
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
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