May 2017
Intermediate to advanced
448 pages
10h 10m
English
The code optimization we've just completed is an example of refactoring--modifying existing code to perform the same task in a more efficient or elegant way. To explore further refactoring opportunities, let's look at the behaviors we have bound to each button. The .removeClass() method's parameter is optional; when omitted, it removes all classes from the element. We can streamline our code a bit by exploiting this as follows:
$(() => { $('#switcher-default') .addClass('selected') .on('click', () => { $('body').removeClass(); }); $('#switcher-narrow') .on('click', () => { $('body') .removeClass() .addClass('narrow'); }); $('#switcher-large') .on('click', () => { $('body') .removeClass() .addClass('large'); ...Read now
Unlock full access