// definition function $(s) { return new lib(s); } function lib(s) { this.$els = document.querySelectorAll(s); } lib.prototype.css = function(prop, val) { this.$els.forEach($el => ($el.style[prop] = val)); // this is what makes it chainable return this; }; lib.prototype.text = function(val) { if (val) { this.$els.forEach($el => ($el.textContent = val)); } else { return Array.from(this.$els) .map($el => $el.textContent) .join(' '); } // this is what makes it chainable return this; }; // usage const texts = $('a').text(); console.log(texts); $('a').css('color', 'red').text('this').text('no that');