Skip to content

Instantly share code, notes, and snippets.

@jakeauyeung
Created July 15, 2014 02:28
Show Gist options
  • Save jakeauyeung/072ace0f390a40f671c0 to your computer and use it in GitHub Desktop.
Save jakeauyeung/072ace0f390a40f671c0 to your computer and use it in GitHub Desktop.
编写一个JS组建
window.dome = (function() {
function Dome(els) {
for(var i = 0; i < els.length; i++) {
this[i] = els[i];
}
this.length = els.length;
}
var dome = {
get: function(selector) {
var els;
if(typeof selector === 'string') {
els = document.querySelectorAll(selector);
} else if(selector.length) {
els = selector;
} else {
els = [selector];
}
return new Dome(els);
}
};
Dome.prototype.map = function(callback) {
var results = [], i = 0;
for(; i< this.length; i++) {
results.push(callback.call(this, this[i], i));
}
return results;
}
Dome.prototype.forEach(callback) {
this.map(callback);
return this;
}
Dome.prototype.someMethod1 = function(callback) {
this.forEach(callback);
return this;
}
Dome.prototype.someMethod2 = function(callback) {
return this.forEach(callback);
}
return dome;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment