Javascript Learning (1) Build your own JS Library

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.