Replacing jQuery's hasClass() in MW extension JS code

Yaron Koren <[email protected]>
Newsgroups gmane.org.wikimedia.mediawiki
Message-ID <CAGmQEQHwkafc0ku=8N+FD-eDOhR_=ZifJ1VB7t_DbT_WtZT=SA@mail.gmail.com>
Hi,

I've been trying to get rid of the ESLint warnings for the JavaScript code
in some of my extensions, when they go through Jenkins validation. One
warning that appears fairly often is this one:

Where possible, maintain application state in JS to avoid slower DOM queries
no-jquery/no-class-state


I'm not sure if this is a warning that's specific to Wikimedia code, but
doing a web search on it brings up this Wikimedia help page as the only
real result:

https://github.com/wikimedia/eslint-plugin-no-jquery/blob/master/docs/rules/no-class-state.md

This page is rather confusing. It says that the warning comes when calling
either hasClass() or toggleClass() on a jQuery element. That part makes
sense, but then the suggested alternatives are strange. The page says that
the following are some examples of bad code:

$( 'div' ).hasClass();
$div.hasClass();

In their place, it suggests the following:

hasClass();
[].hasClass();
div.hasClass();

Can anyone explain this? As far as I'm aware, hasClass() is only defined in
jQuery - and there, only as a method for variables representing jQuery
elements. So what do those "recommended" lines mean? And in general, what
is the best way to determine whether some element on the page has a certain
class?

-Yaron

-- 
WikiWorks · MediaWiki Consulting · http://wikiworks.com

_______________________________________________
MediaWiki-l mailing list -- [email protected]
List information: https://lists.wikimedia.org/postorius/lists/mediawiki-l.lists.wikimedia.org/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.