Why write code Jquery like this?
(function ($) {
$(function () {
.......
});
})(jQuery);
HTML. The this Keyword is a reference to DOM elements of invocation. We can call all DOM methods on it. $() is a jQuery constructor and in $(this), we are just passing this as a parameter so that we can use the jQuery function and methods.
jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is easier to use compared to JavaScript and its other JavaScript libraries. You need to write fewer lines of code while using jQuery, in comparison with JavaScript.
JQuery is useful, but what killed it was it's widespread use. Vanilla Javascript kept updating so that things that made jQuery special became things that you found in Javascript without jQuery. So, it started becoming obsolete.
jQuery is Easy to Learn In addition to extending the functionality of your pages, jQuery helps your code stay simple, clean, and easy to read. Legible code is essential when you're getting started, as it makes it easier to spot your mistakes!
This is called a closure to avoid conflicts with other libraries which are using $
. This way you can ensure to use $
in that function with passing jQuery
as a param.
(function ($) {
$(function () {
.......
});
})(jQuery); //<----passing jquery to avoid any conflict with other libraries.
it's a best practice to pass jQuery to an IIFE (Immediately Invoked Function Expression) that maps it to the dollar sign so it can't be overwritten by another library in the scope of its execution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With