Foreword Library is a hot and controversial topic. One view is that it is a great tool that is indispensable for any developer; the other view is that relying on a library without understanding its inner workings promotes laziness. This leads to a decline in the quality of developers. But no matter what, everyone seems to agree with writing their own libraries, and merging the things they use daily is a meaningful thing in itself.
Next we start to build our own JS library and write two of our own methods
//Author:
//Time: 2012-11-13
(function(){
window['LS']={};
function $(){
var elements =new Array();
var element;
for(var i=0;i
if(typeof(arguments[i])=="string"){
element=document.getElementById(arguments[i]);
}
if(arguments.length==1){
return element;
}
elements.push(element);
}
return elements;
}
window['LS']['$']= $;
function getElementByClassName(className,tag){
var allTags=document.getElementsByTagName(tag);
var matchingElements=new Array();
className=className. replace(/-/g,"\-"); //I don't understand what this sentence means.
var regex=new RegExp("(^|\s)*" className "(\s|$) ");
var element;
for(var i=0;ielement=allTags[i];
if(regex .test(element.className)){ //I don’t understand what this element.className means either
matchingElements.push(element);
}
}
return matchingElements;
}
window['LS']['getElementByClassName']=getElementByClassName;
})() //If you don’t add (), it cannot be executed
I am new to javascript, and there are comments in it I don't understand part of it, and I hope someone can give me some advice.