Created
August 29, 2015 23:02
-
-
Save segdeha/90ae3d809d23b6aaac21 to your computer and use it in GitHub Desktop.
Simple example of chaining
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Allow chaining of classList methods | |
* @usage klass( document.querySelector('#my-view') ).remove( 'hidden' ).adds( ['active', 'loading'] ); | |
*/ | |
function klass(el) { | |
var api = (function () { | |
return { | |
add : function (klass) { | |
el.classList.add(klass); | |
return api; | |
}, | |
remove : function (klass) { | |
el.classList.remove(klass); | |
return api; | |
}, | |
toggle : function (klass) { | |
el.classList.toggle(klass); | |
return api; | |
}, | |
adds : function (klasses) { | |
klasses.forEach(function (klass) { | |
api.add(klass); | |
}); | |
return api; | |
}, | |
removes : function (klasses) { | |
klasses.forEach(function (klass) { | |
api.remove(klass); | |
}); | |
return api; | |
}, | |
toggles : function (klasses) { | |
klasses.forEach(function (klass) { | |
api.toggle(klass); | |
}); | |
return api; | |
} | |
}; | |
}()); | |
return api; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment