Skip to content

Instantly share code, notes, and snippets.

@caiotarifa
Created March 31, 2017 18:26
Show Gist options
  • Save caiotarifa/73812569aa7ad0a1b1a64e72a66608d1 to your computer and use it in GitHub Desktop.
Save caiotarifa/73812569aa7ad0a1b1a64e72a66608d1 to your computer and use it in GitHub Desktop.
var BigCoffee = function(selector) {
var elements = document.querySelectorAll(selector),
index = 0;
if (this === window) {
return new BigCoffee(selector);
}
for (; index < elements.length; index++) {
this[index] = elements[index];
}
this.length = elements.length;
return this;
};
BigCoffee.prototype = {
map: function(callback) {
var results = [],
index = 0;
for (; index < this.length; index++) {
results.push(callback.call(this, this[index], index));
}
return results;
},
forEach: function(callback) {
this.map(callback);
return this;
},
// CLASSES
addClass: function(className) {
return this.forEach(function(element) {
element.classList.add(className);
});
},
hasClass: function() {
return 105;
},
removeClass: function(className) {
return this.forEach(function(element) {
element.classList.remove(className);
});
},
// ATTRIBUTES
getAttribute: function(attribute) {
return this.forEach(function(element) {
element.getAttribute(attribute);
});
},
removeAttribute: function(attribute) {
return this.forEach(function(element) {
element.removeAttribute(attribute);
});
},
setAttribute: function(attribute, value) {
return this.forEach(function(element) {
element.setAttribute(attribute, value);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment