I made this little library for personal use to make javascripting faster and easier. I would like to know if there are any inefficiencies in it (in size and performance) and possibly if there is a feature you think is missing that would be useful.
//PUT ALL CODE AT THE END OF THE PAGE!!!!!! THIS IS UBER IMPORTANT
//document shorthand
doc=document
//querySelectorAll shorthand
function $q(a){return doc.querySelectorAll(a)}
//returns specific element
function $(a,b){return $q(a)[b||0]}
//function to set properties in a chainable manner
Element.prototype.$p=function(a){for(b in a)this[b]=a[b];return this}
//creates a class that hides elements (notice the use of the function above)
$("head").appendChild(doc.createElement("style").$p({"innerHTML":".wqh{display:none}"}))
Element.prototype.$p({
"removeClass":function(a){this.classList.remove(a)},
"addClass":function(a){this.classList.add(a)},
"hide":function(){this.addClass("wqh")},
"show":function(){this.removeClass("wqh")},
//loads content of a file inside the element, example: element.load("random file.txt")
"load":function(a,b,c){c=this;b=new XMLHttpRequest;b.open("GET",a,!0);b.send();b.onreadystatechange=function(){c.innerHTML=b.responseText}}
})
Example of its uses:
$(".test").addClass("red")
$(".test",1).removeClass("big")
$(".test",2).hide()
for(a in b=$q("p"))
b[a].style.fontFamily="serif",
b[a].style.color="blue",
b[a].style.fontSize="20px";
More readable version of the $p() function:
//function to set properties in a chainable manner
Element.prototype.$p = function(proplist){
var property;
for(property in proplist){this[property] = proplist[property]}
return this;
}
$
is already used by other libraries. It is also a meaningless name for a function. Please choose something else. 4)querySelectorAll
is really slow, and missing from old browsers. You can actually get better performance by writing your own selector routine. 5) Adding properties to built-in constructors is a bad practice. See prototype.js. \$\endgroup\$