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(' '); if(findInArr(sClass,tmp) == true){ arr.push(aEle[i]); } } return arr; } }
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>
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>
3. Multiplication table
<script> document.write('<table>'); for(var i = 1; i <= 9; i++){ document.write('<tr>'); for(var j = 1; j <= i; j++){ document.write('<td>'+i+'*'+j+'='+i*j+'</td>'); } document.write('</tr>'); } document.write('</table>'); </script>
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+'='+json[name]) }; //alert(arr); var str=arr.join('&'); alert(str); </script>
【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!