Write 19 guidelines for effective jquery code

  • 2020-03-30 02:25:40
  • OfStack

Discard deprecating methods
It is important to keep an eye on the discard methods for each new release and try to avoid them.


//Oops - live has been scrapped
$('#stuff').live('click', function() {
  console.log('hooray');
});
//  advice 
$('#stuff').on('click', function() {
  console.log('hooray');
});

// note: this may not be appropriate, because live can implement real-time binding, delegate may be more appropriate
17. Use the CDN
Google's CND ensures that the cache closest to the user is selected and responds quickly. (please search the address by yourself if you use Google CND, the address here is not available, recommend the CDN provided by jquery official website).

Combine jQuery and javascript native code when necessary
As mentioned above, jQuery is javascript, which means that what you can do with jQuery, you can also do with native code. Native code (or vanilla) is probably not as readable and maintainable as jQuery, and the code is longer. But it also means being more efficient (usually the closer to the underlying code, the less readable it is, the higher the performance, for example: assembly, which of course requires more powerful talent). Keep in mind that no framework is smaller, lighter, and more efficient than native code

Given the performance differences between vanilla and jQuery, I strongly recommend taking the best of both worlds and using (if possible) native code equivalent to jQuery.

19. Final advice
Finally, my goal in documenting this article is to improve the performance of jQuery and some other good Suggestions. If you want to delve into this topic you will find a lot of fun. Remember, jQuery is not a must-have, just an option. Think about why you're using it. DOM manipulation? Ajax? Template? CSS animations? Selector engine? Perhaps a custom version of the javascript mini-framework or jQuery would be a better choice.


Related articles: