-
-
Save jakeauyeung/072ace0f390a40f671c0 to your computer and use it in GitHub Desktop.
编写一个JS组建
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
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