Preface
Library is a controversial topic. One idea is that it is a great tool and indispensable to any developer; another idea is that the dependency on the database without the internal working principle of the database can contribute to the laziness and lead to a decline in the quality of developers. But in any case, it seems that everyone agrees to write their own libraries. Besides, it is meaningful to merge the things they use in daily life together.
Next we will start to build our own JS library and write two methods of our own.
Copy codeThe Code is as follows:
// Prepared:
// Time:
(Function (){
Window ['LS'] = {};
Function $ (){
Var elements = new Array ();
Var element;
For (var I = 0; I <arguments. length; 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, "\-"); // you do not understand what this sentence means.
Var regex = new RegExp ("(^ | \ s) *" + className + "(\ s | $ )");
Var element;
For (var I = 0; I <allTags. length; I ++ ){
Element = allTags [I];
If (regex. test (element. className) {// What does this element. className mean?
MatchingElements. push (element );
}
}
Return matchingElements;
}
Window ['LS'] ['getelementbyclassname'] = getElementByClassName;
}) () // Cannot be executed ()
I was just getting started with javascript, and the comments in it are something I don't understand. I also hope you can give me some advice.