JavaScript Learning (a) to build their own JS library _ basic Knowledge

Source: Internet
Author: User
Preface
The library is a hotly debated topic. One view is that it is a great tool and indispensable to any developer, while another view is that relying on the library without understanding the internal workings of the library can encourage laziness and lead to a decline in developer quality. But no matter what, it seems that everyone agrees to write their own library, and it's a meaningful thing to combine what they use everyday.

Below we start to build our own JS library, and write two of their own methods
Copy Code code as follows:

Author:
Time: 2012-11-13
(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, "\\-"); I don't 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)) {//This element.classname does not understand what it means
Matchingelements.push (Element);
}
}
return matchingelements;
}
window[' LS ' [' Getelementbyclassname ']=getelementbyclassname;
} ()///If no () cannot be performed

I was just beginning to contact with JavaScript, which is part of the annotation is I do not understand the place, but also hope that the expert can guide.

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.