How can I load a JavaScript file only when a page has an element whose class is "test"? In my develop process, I meet a scenario: I only want to load a JavaScript file when a page has an element whose class is test, so what can I do? How to know a page whether has this element?
Well one way would be to just add the script always and check for the existence of test class there.
(function ($, Drupal) {
    Drupal.behaviors.tester = {

        attach: function (context) {
           if (context.querySelector('.test')) {
               // Do something
               // Or maybe load another JS file dynamically
           }   
        }

})(jQuery, Drupal);