JavaScript Note 2 Array and Date Object Methods_Javascript Skills
Object Basics of JavaScript
This article mainly explains the various methods of local objects Array and Date.
An object-oriented language needs to provide developers with four basic capabilities:
(1) Encapsulation - the ability to store relevant information (whether data or methods) in objects.
(2) Aggregation - the ability to store one object within another object.
(3) Inheritance - the ability to obtain the properties and methods of a class from another class (or classes).
(4) Polymorphism - The ability to write functions or methods that can be run in multiple ways.
1. Local objects include:
①Array class
②Date class
2. Built-in objects
3. Host object
Array class
toString() method and valueOf() method return a special string. The string is constructed by calling the toString() method on each item and then concatenating them with commas. For example, calling the toString() method or the valueOf() method on an array with items "red", "green", and "blue" returns the string "red, green, blue".
The only purpose of the join() method is to join string values. The join() method has only one parameter, which is the string used between array items.
The split() method converts a string into an array. The split() method has only one parameter, which is the string that is regarded as the separator between array items. If the empty string is declared as the separator, each item in the array returned by the split() method is a character of the string.
The concat() method works with arrays almost exactly the same way it works with strings. The parameters will be appended to the end of the array, and the returned function value is a new Array object (including the items in the original array and the new items). The
slice() method accepts one or two parameters, the starting position and the ending position of the item to be extracted. If there is only one parameter, this method will return all items starting from this position to the end of the array; if there are two parameters, this method will return all items between the first position and the second position, excluding the second position. item at.
unshift() method, which places an item at the first position of the array and then moves the remaining items down one position.
reverse() method reverses the order of array items. The
sort() method will sort the array items in ascending order based on their values. (Note: This is just sorting of string codes, sorting of numeric arrays requires another solution)
The splice() method is the most complex method, inserting data items into the middle of the array.
1. Delete - You only need to declare two parameters to delete any number of items from the array. These two parameters are the position of the first item to be deleted and the number of the items to be deleted. number. For example, arr.splice(0,2) will delete the first two items in the array arr.
2. Replace without deleting - You can insert the data item into the specified position by declaring three parameters. These three parameters are the starting position, 0 (the number of array items to be deleted) and the number of array items to be deleted. The inserted item. Additionally, you can specify additional items to insert using the fourth, fifth, or more parameters. For example, arr.splice(2,0,"red", "green") will insert "red" and "green" at position 2.
3. Replace and delete - You can insert the data item into the specified position by declaring three parameters. These three parameters are the starting position, the number of array items to be deleted, and the item to be inserted. Additionally, you can specify more items to insert. The number of items to be inserted does not have to equal the number of items to be deleted. For example, arr.splice(2,1, "red","green") will delete the item at position 2 in the array arr and then insert "red" and "green" at position 2.
Concepts of stack and queue
Difference 1
Stack: Last-in-first-out (LIFO) structure, items added first are deleted first, and items in the stack are deleted first. Insertions and deletions only occur at the top of the stack.
Queue: First-in-first-out (FIFO) structure, items added first are deleted last. Insertion operations of elements only occur at the end of the queue, while deletion operations occur at the head of the queue.
Difference 2
Stack: adding an item at the top is called "push into the stack", and deleting the top item is called "popping out of the stack".
Queue: adding an item at the end of the queue is called "put" or "enqueue", and deleting an item at the head of the queue is called "get" or "dequeuing".
在Array类中的运用
栈:push()方法用于在Array结尾添加一个或多个项,pop()方法用于删除最后一个数组项(length-1),返回它作为函数值。
队列:push()方法把数据项加入队列(即在数组结尾添加数据项),shift()方法将删除数组中的第一个项,将其作为函数值返回。
Date类
Date类的方法(列在下表中)均用于设置或获取日期值的某部分。
方 法 |
说 明 |
toLocaleDateString() |
以地点特定的格式显示Date的时间部分 |
getTime() |
返回日期的毫秒表示 |
setTime(milliseconds) |
设置日期的毫秒表示 |
getFullYear() |
返回用四位数字表示的日期的年份(如2004而不只是04) |
getUTCFullYear() |
返回用四位数字表示的UTC日期的年份 |
setFullYear(year) |
设置日期的年份,参数必须是四位数字的年份值 |
setUTCFullYear(year) |
设置UTC日期的年份,参数必须是四位数字的年份值 |
getMonth() |
返回日期的月份值,由数字0(1月)到11(12月)表示 |
getUTCMonth() |
返回UTC日期的月份值,由数字0(1月)到11(12月)表示 |
setMonth(month) |
设置日期的月份为大于等于0的数字。对于大于11的数字,开始累计年数 |
setUTCMonth(month) |
设置UTC日期的月份为大于等于0的数字。对于大于11的数字,开始累计年数 |
getDate() |
返回该日期该月中的某天 |
getUTCDate() |
返回该UTC日期该月中的某天 |
setDate(date) |
设置该日期该月中的某天 |
setUTCDate(date) |
设置该UTC日期该月中的某天 |
getDay() |
返回该日期为星期几 |
getUTCDay() |
返回该UTC日期为星期几 |
setDay(day) |
设置该日期为星期几 |
setUTCDay(day) |
设置该UTC日期为星期几 |
getHours() |
返回日期中的小时值 |
getUTCHours() |
返回UTC日期中的小时值 |
setHours(hours) |
设置日期中的小时值 |
setUTCHours(hours) |
设置UTC日期中的小时值 |
getMinutes() |
返回日期中的分钟值 |
getUTCMinutes() |
返回UTC日期中的分钟值 |
setMinutes(minutes) |
设置日期中的分钟值 |
setUTCMinutes(minutes) |
设置UTC日期中的分钟值 |
getSeconds() |
返回日期中的秒值 |
getUTCSeconds () |
返回UTC日期中的秒值 |
setSeconds (seconds) |
设置日期中的秒值 |
setUTCSeconds (seconds) |
设置UTC日期中的秒值 |
getMilliseconds() |
返回日期中的毫秒值。注意,这不是自1970年1月1日以后的毫秒值,而是当前时间中的毫秒值,例如4 :55 :34.20,其中20即为时间的毫秒值 |
getUTCMilliseconds () |
返回UTC日期中的毫秒值 |
setMilliseconds (milliseconds) |
Set the millisecond value in the date |
setUTCMilliseconds (milliseconds) |
Set UTCThe millisecond value in the date |

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

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



If you're looking for a way to automatically create and name files and folders based on system timestamps, you've come to the right place. There is a super simple way to accomplish this task. The created folders or files can then be used for various purposes such as storing file backups, sorting files based on date, etc. In this article, we will explain in some very simple steps how to automatically create files and folders in Windows 11/10 and name them according to the system’s timestamp. The method used is a batch script, which is very simple. Hope you enjoyed reading this article. Section 1: How to automatically create and name a folder based on the current timestamp of the system Step 1: First, navigate to the parent folder where you want to create the folder,

When developing using PHP programs, you often encounter some warning or error messages. Among them, one error message that may appear is: PHPWarning:date()expectsparameter2tobelong,stringgiven. The error message means: the second parameter of the function date() is expected to be a long integer (long), but what is actually passed to it is a string (string). So, we

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

1. Introduction The Date class in the java.util package represents a specific time, accurate to milliseconds. If we want to use our Date class, then we must introduce our Date class. Writing the year directly into the Date class will not produce the correct result. Because Date in Java is calculated from 1900, so as long as you fill in the first parameter with the number of years since 1900, you will get the year you want. The month needs to be subtracted by 1, and the day can be inserted directly. This method is rarely used, and the second method is commonly used. This method is to convert a string that conforms to a specific format, such as yyyy-MM-dd, into Date type data. First, define an object of Date type Date

How to get millisecond representation of date using getTime() method of Date class In Java, Date class is a class used to represent date and time. It provides many useful methods to manipulate and obtain information about date objects. Among them, the getTime() method is an important method in the Date class, which can return the millisecond representation of the date object. Next, we will detail how to use this method to obtain the millisecond representation of a date, and provide corresponding code examples. Using the Date class

There are many excellent calendar libraries and date libraries in Python for us to use. These libraries can help us handle date and calendar related operations. Next, I will introduce you to several common choices and provide corresponding code examples. Datetime library: Datetime is Python's built-in date and time processing module. It provides many date and time related classes and methods, which can be used to process dates, times, time differences and other operations. Sample code: importdatetime#Get the current date

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

Stringbuild class Since the object content of the String class cannot be changed, a new String object will be constructed every time it is spliced, which is time-consuming and wastes memory space. At this time, you need to solve this problem through the StringBuild class provided by Java. StringBuilder is also called a variable character sequence. , it is a string buffer similar to String, which can be regarded as a container. Many strings can be held in the container. Variable means that the content in the StringBuilder object is variable. The construction method publicStringBuilder(): creates an empty buffer publicStringBuilder(Stringsr
