Skip to content

Instantly share code, notes, and snippets.

@oslego
Created October 31, 2012 22:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oslego/3990424 to your computer and use it in GitHub Desktop.
Save oslego/3990424 to your computer and use it in GitHub Desktop.
Small jquery-like experiment
var $ = function(){
function noQuery() {
var args = Array.prototype.slice.call(arguments, 0)[0],
query = args[0]
qsa = function(q){
return Array.prototype.slice.call(document.querySelectorAll(q), 0)
}
this.el = []
if (typeof query == 'string'){
// create element
if (/^<([a-z])+>$/.test(query)){
var el = document.createElement(query.substr(1, query.length-2))
this.el.push(el)
}
// query selector
else{
this.el = qsa(query)
}
}
}
noQuery.prototype.each = function(arr, iterator){
arr.forEach(iterator)
}
noQuery.prototype.on = function(ev, fn){
this.each(this.el, function(el){
el.addEventListener(ev, fn)
})
return this
}
noQuery.prototype.off = function(ev, fn) {
this.each(this.el, function(el){
el.removeEventListener(ev, fn)
})
return this
}
noQuery.prototype.css = function(prop, value){
this.el.forEach(function(el){
})
return this
}
noQuery.prototype.attr = function(prop, value){
if (arguments.length == 1){
if (typeof prop == 'string'){
this.each(this.el, function(el){
return el.getAttribute(prop)
})
}
}
return this
}
return new noQuery(arguments)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment