JavaScript notes two array and date object methods _javascript Tips

Source: Internet
Author: User

The object base of JavaScript

This article mainly explains the various methods of the local object array and date.

An object-oriented language needs to provide developers with four basic capabilities:

(1) Encapsulation-the ability to store relevant information, regardless of data or method, in an object.
(2) aggregation-the ability to store an object within another object.
(3) Inheritance-the ability of a class to be derived from another class (or classes) by its properties and methods.
(4) Polymorphism-the ability to write functions or methods that can be run in a variety of ways.

1. Local objects include:

①array class
②date class

2. Built-in Objects
3. Host Object
Array Class
The ToString () method and the ValueOf () method return a special string. The string is formed by invoking the ToString () method on each item and then connecting them together with commas. For example, you call the ToString () method or the ValueOf () method for an array with item "Red", "green", and "blue", and the string "Red,green,blue" is returned.

The only use of the join () method is the connection string value. The join () method has only one parameter, the string used between the array items.
The split () method converts the string to an array, and the split () method has only one argument, which is the string that is considered to be the delimiter between the arrays. If an empty string is declared as a delimiter, each item in the array returned by the split () method is the character of the string.

The Concat () method handles arrays almost exactly the way it handles strings. The parameter is appended to the end of the array, and the returned function value is the new array object, including the items in the original array and the new item.

The slice () method accepts one or two arguments, the starting and ending positions of the items to extract. If there is only one argument, the method returns all the items starting at that point to the end of the array, and if there are two parameters, the method returns all items between the first position and the second position, excluding the item at the second position.

Unshift () method, which places an item in the first position of the array, and then moves the remaining items down one position.

The reverse () method reverses the order of the array items.

The sort () method sorts the values of the array items according to their value in ascending order. (Note: Just the sort of string code, the sorting of the numeric array requires another solution)

The most complex method of the splice () method is to insert the data item into the middle of the array.

1. Delete--just declare two parameters, you can delete any number of items from the array, the two parameters are the location of the first item to be deleted and the number of items to delete. For example, Arr.splice (0,2) deletes the first two items in the array arr.

2. Replace without deleting--declaring three parameters allows you to insert data items into the specified location, three parameters are the starting position, 0 (the number of array items to delete), and the item to insert. In addition, you can specify additional items to insert with the fourth, fifth, or more parameters. For example, Arr.splice (2,0, "Red", "green") inserts "Red" and "green" at position 2.

3. Replace and delete--declare three parameters to insert the data item into the specified position, the three parameters are the starting position, the number of array items to delete, and the item to insert. In addition, you can specify more items to insert. The number of items to insert does not have to be equal to the number of items that are deleted. For example, Arr.splice (2,1, "Red", "green") deletes the item at position 2 in the array arr, and then inserts "Red" and "green" at position 2.

The concept of stacks and queues

distinguish one

Stack: LIFO (LIFO) structure, the first additions are deleted, and the inserts and deletions in the stack only occur at the top of the stack.
Queue: first-in First Out (FIFO) structure, items added first are finally deleted, and element inserts occur only at the end of the queue, while deletions occur on the head of the queue.

Difference Two

Stack: Top Add item called "Push Stack", delete top item called "Pop-up stack".
Queues: Team tails Add items called "put" or "team", and teams head delete items called "get" or "out team".

The application of the array class

Stack: The push () method is used to add one or more items at the end of the array, and the Pop () method is used to delete the last array item (LENGTH-1) and return it as a function value.

Queues: Push () method adds data items to the queue (that is, adding data items at the end of the array), the Shift () method deletes the first item in the array and returns it as a function value.

Date Class

The method of the date class (listed in the following table) is used to set or get a part of the date value.

Square Law

Say

toLocaleDateString ()

Displays the time part of date in a location-specific format

GetTime ()

Returns the millisecond representation of the date

SetTime (milliseconds)

Set the millisecond representation of a date

getFullYear ()

Returns the year of a date with a four-digit number (for example, instead of just one )

getUTCFullYear ()

Returns the year of the UTC date in four-digit numbers

setFullYear (Year)

Set the year of the date, the parameter must be a four-digit year value

setUTCFullYear (Year)

To set the year for the UTC date, the parameter must be a four-digit year value

GetMonth ()

Returns the month value of the date, represented by the number 0(1 months) to one ( month)

getUTCMonth ()

Returns the month value of the UTC date, represented by the number 0(1 months) to one ( month)

Setmonth (month)

Set the month of the date to a number greater than or equal to 0 . For more than one number, start the cumulative number of years

setUTCMonth (month)

Set a UTC date with a number greater than or equal to 0 . For more than one number, start the cumulative number of years

GetDate ()

Returns the day of the month for that date

getUTCDate ()

Returns the day of the month in which the UTC date

Setdate (date)

Set the day of the month for that date

setUTCDate (date)

Set this UTC date one day of the month

Getday ()

Returns the day of the week

getUTCDay ()

Returns the day of the week in UTC

Setday (Day)

Set the day of the week

Setutcday (Day)

Set the UTC date to the day of the week

GetHours ()

Return a small value in a date

getUTCHours ()

Returns the value of a small value in a UTC date

Sethours (hours)

Set a small value in a date

setUTCHours (hours)

Set a time value in UTC date

Getminutes ()

Returns the minutes value in a date

getUTCMinutes ()

Returns the minutes value in the UTC date

Setminutes (minutes)

Set the minutes value in a date

setUTCMinutes (minutes)

Set The minutes value in a UTC date

Getseconds ()

Returns the value of a second in a date

getUTCSeconds ()

Returns the second value in the UTC date

Setseconds (seconds)

Set the value of a second in a date

setUTCSeconds (seconds)

Set The second value in the UTC date

Getmilliseconds ()

Returns the millisecond value in the date. Note that this is not a millisecond value since the 1970 1 months 1 days, but a millisecond value in the current time, such as 4:55:34.20, where the millisecond value of the time

getUTCMilliseconds ()

Returns the millisecond value in the UTC date

Setmilliseconds (milliseconds)

Set the millisecond value in a date

setUTCMilliseconds (milliseconds)

Set The millisecond value in the UTC date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.