This article mainly shares with you a brief analysis of JS dates, Math, arrays and objects, hoping to help everyone.
Related questions
Get the date in 2017-06-10 format
function formatDate(dt){ if(!dt){ dt = new Date(); } var year = dt.getFullYear();var month = dt.getMonth()+1;var date = dt.getDate();if(month
Get random Number, the requirement is a string format of consistent length (the role of random numbers in front-end development)
var random =Math.random();random= random + '0000000000';//保证下一句不会报错random = random.slice(0,9); console.log(random);
Write a general forEach that can traverse objects and arrays Function (available in jquery)
//想想对象中的forEach与数组中的for in怎么用比较好//这里是forEach函数function forEach(obj,fn){ var key; if(obj instanceof Array){ obj.forEach(function(item,index){ fn(index.item); }) }else{ for(key in obj){ fn(key.obj[key]); } } }//调用var arr = [1,2,3];forEach(arr,function(index,item)){ console.log(index.item); }var obj = {x:100,y:200};forEach(obj,function(key,value){ console.log(key,value); })
Knowledge point
Date
Date.nowTime(); //获取当前时间毫秒数(这个数是从1970开始算起到现在走了多少毫秒数)var myDate = new Date(); //new一个时间对象myDate.getTime(); //获取毫秒数myDate.getFullYear(); //年(4位数),还有一种getYear()的方法,有兼容性的问题,与getFullYear()相差1900myDate.getMonth(); //月(0-11),实际应用是应该注意它的返回值myDate.getDate();//日(1-31)myDate.getDay();//星期(0-6),实际应用是应该注意它的返回值myDate.getHours();//时(0-23)myDate.getMinutes();//分(0-59)myDate.getSeconds();//秒(0-59)
-
Math
1. Get a random number Math.random();
will return a decimal number greater than 0 and less than 1, eg: 0.14249200181060218, which is generally used to clear cache For example: when the page is opened, due to the existence of cache, the newly updated data sometimes cannot be refreshed on the page, because if the requested address is the same, the browser will not connect to the server. At this time, random numbers can be used to process it, which is equivalent to loading a new page.
在URL 参数后加上 "?r=" + Math.random();//加随机数在 URL 参数后加上 "?timestamp=" + new Date().getTime(); //也可以加时间戳
2. For more methods and random number extensions, see my previous blog;
Array API
1. forEach traverses all elements
var arr = [1,2,3]; arr.forEach(function(item,index){//item为元素的值eg:1,2,3,index位为元素的位置,从0开始的 //遍历数组中所有元素 console.log(index,item); //注意参数顺序});
2 . every determines whether all elements meet the conditions
var arr = [1,2,3];var result = arr.every(funciton(item,index){// 用来判断所有的数组元素,都满足一个条件 if(item <p>3 . some determines whether at least one element meets the conditions </p><pre class="brush:php;toolbar:false">var arr = [1,2,3];var result = arr.every(funciton(item,index){// 用来判断所有的数组元素,都满足一个条件 if(item <p>4 . sort Sort (from small to large) Sort toward larger) </p><pre class="brush:php;toolbar:false">var arr = [1,4,3,2,5];var arr2 = arr.sort(function(a,b){ //从小到大 return a-b; //从大到小 //return b-a;}); console.log(arr2);//[1,2,3,4,5]
5. map reassembles the elements to generate a new array (assembly of elements into another type of elements according to a rule to generate a new array)
var arr = [1,2,3,4];var arr2 = arr.map(function(item,index){ //将元素重新组装并返回 return '<b>'+item+'</b>'; });console.log(arr2); //["<b>1</b>", "<b>2</b>", "<b>3</b>", "<b>4</b>"]
6. Filter elements that meet the conditions
var arr = [1,2,3];var arr2 = arr.filter(function(item,index){ //通过一个条件过滤数组 if(item>=2){ return true; } }); console.log(arr2); //2,3
Object API
var obj ={x:100,y:200,z:300};var key;for(key in obj){//key这里取得是obj的属性名 if(obj.hasOwnProperty(key)){//判断他是这个obj的属性而不是原型的属性 console.log(key,obj[key]); } }
Related recommendations:
JS date object simple operation (Get the current year, week, and time)
JS date addition and subtraction function sorting
js date related function summary sharing_javascript skills
The above is the detailed content of A brief analysis of JS dates, Math, arrays and objects. For more information, please follow other related articles on the PHP Chinese website!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool