I was recently listening to a podcast which made a comment on using $()
vs using jQuery()
. It was stated that every time $()
was used a new object would be created and when jQuery()
was used this was not the case. I google'd around but couldn't find anything on this specific topic.
I realize this is not a typical example, but the following is the reason I am interested in the answer to this question.
I have a page that the user will keep loaded in a browser for a whole day (24 hours, or possibly longer) and updates are done to the DOM every ~5 seconds as the result of an AJAX call via jQuery (the AJAX call portion is irrelevant to updating the DOM - the update to the DOM is done using a string of HTML and a call on a jQuery object to .empty()
and then .html()
).
Since hearing this, I subsequently switched all of the $()
calls to jQuery()
calls, but I would like to know:
Is using $()
vs using jQuery()
a bad practice? Is there a negligible difference between the two? Or is it actually noticeable on larger projects?
You should never include jQuery in an AngularJS app. Doing so is bad practice. However, if you are using some oldschool jQuery plugin wrapped into directives, and by that have jQuery included, it is perfectly OK to use jQuery when you deal with the plugin under the hood, i.e need to target properties etc.
The value of this inside a click event is a DOM element (the one that was clicked). Using $(this) converts it to a jQuery object. DOM elements do not have a hide() methods, but jQuery adds that and many other methods you can then call.
In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.
In most cases the answer is no. It is actually a big problem in the open source world today as so many projects have simply been abandoned because the owner had no financial incentive to continue to maintain the code.
No, it's not bad practice, and there is no performance difference.
The $
and jQuery
identifiers refer to the same function instance.
This is done by the last line of jQuery.js:
window.jQuery = window.$ = jQuery;
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