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

Introducing several encapsulation functions in javascript-ecma

伊谢尔伦
Release: 2017-05-21 09:50:42
Original
1502 people have browsed it

First introduce the following methods:

String method:

str.length str.charAt(i): Get a certain value in the string a;

str.indexOf('e'); Find the first occurrence; if not found, return -1;

str.lastIndexOf('e'): Find the last one The position where e appears; cannot be found and returns -1;

str.toUpperCase(); Convert to uppercase str.toLowerCase(); Convert to lowercase str.substring (starting position, ending position): characters String interception; str.split('cutting method'); string cutting; string conversionarray;


array method:
Delete: arr.splice (starting position, number of deleted items); Add: arr.splice (starting position [which bit to add in front], 0, element 1····); Replacement: arr.splice (starting position, number of deleted items, element 1···); arr.sort(function(n1,n2){return n1 - n2}): sort arr.reverse(): flip arr.push (); add one digit to the back of the array; arr.unshift(): add one digit to the front of the array; arr.pop(): delete one digit from the back of the array; arr.shift(): delete one digit from the front of the array ; arr.join('Connection method'); Method of converting array to string; arr.concat(arr1, arr2····); Array connection;

Mathematical method:

Math.random(); Random number; Math.round(): Rounding; Math.ceil() Rounding up; Math.floor() Rounding down; Math.abs() : Take the absolute value; Math.max(x,y): Find the maximum number; Math.min(x,y); Find the minimum number; Math.pow(x,y): How many powers of a number; Math.sqrt( ): square root;

Array: store multiple things; json: store multiple things; json = { //Key-value pairs name1[key,key]:value1[value,value], name2[key ,key]:value2[value,value], }

The difference between json and arr:

1: length undefined arr.length;

2: Subscript attribute method arr[0];

3: Order No order With order;

4: Loop for in for,while,for in

for The in loop can also loop through arrays, but it is not recommended because the performance is slightly lower;

JS small features: comma expression: only look at the value after the last comma;

The following is a small function that obtains element encapsulation through class:

//getByClass(oParent,sClass);//oParent:从哪个父级下面获取;sClass:要获取的是哪个class名字;
function getByClass(oParent,sClass){
    if(oParent.getElementsByClassName){
        return oParent.getElementsByClassName(sClass);
    }else{
        var aEle = oParent.getElementsByTagName('*');
        var arr = [];
        for(var i = 0; i < aEle.length; i++){
            var tmp = aEle[i].className.split(&#39; &#39;);
            if(findInArr(sClass,tmp) == true){
                arr.push(aEle[i]); 
            }
        }
        return arr;
    }
} 
Copy after login

Here are a few more small cases:

1. How many days are there in this month

<script>
        var oDate=new Date();
        oDate.setMonth(oDate.getMonth()+1);
        oDate.setDate(0);
        alert(oDate.getDate())
</script>
Copy after login

2. What day of the week is the first day of this month

<script>
   var oDate=new Date();
   oDate.setDate(1);
   alert(oDate.getDay());
</script>
Copy after login

3. Multiplication table

<script>
    document.write(&#39;<table>&#39;);
    for(var i = 1; i <= 9; i++){
        document.write(&#39;<tr>&#39;);
        for(var j = 1; j <= i; j++){
            document.write(&#39;<td>&#39;+i+&#39;*&#39;+j+&#39;=&#39;+i*j+&#39;</td>&#39;);
        }
        document.write(&#39;</tr>&#39;);
    }
    document.write(&#39;</table>&#39;);
</script>
Copy after login

4. Convert json to string

<script>
    var json={a:12,b:5,c:9,d:6};
    var arr=[];
    for(var name in json){
        arr.push(name+&#39;=&#39;+json[name])
    };
    //alert(arr);
    var str=arr.join(&#39;&&#39;);
    alert(str);
</script>
Copy after login



【Related recommendations】

1. Free js online video tutorial

2. JavaScript Chinese Reference Manual

3. php.cn Dugu Jiujian (3) - JavaScript video tutorial

The above is the detailed content of Introducing several encapsulation functions in javascript-ecma. For more information, please follow other related articles on the PHP Chinese website!

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