Home > Web Front-end > JS Tutorial > body text

JavaScript learning (1) Build your own JS library_Basic knowledge

WBOY
Release: 2016-05-16 17:44:37
Original
985 people have browsed it
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
Copy the code The code is as follows :

//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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template