Home  >  Article  >  Web Front-end  >  JavaScript Note 2 Array and Date Object Methods_Javascript Skills

JavaScript Note 2 Array and Date Object Methods_Javascript Skills

WBOY
WBOYOriginal
2016-05-16 18:26:401240browse

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()

返回日期的月份值,由数字01月)到1112月)表示

getUTCMonth()

返回UTC日期的月份值,由数字01月)到1112月)表示

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()

返回日期中的毫秒值。注意,这不是自197011日以后的毫秒值,而是当前时间中的毫秒值,例如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

Statement:
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