Introducing several encapsulation functions in javascript-ecma
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
