Built-in objects for JavaScript

Source: Internet
Author: User
Tags first string

Built-in objects in JavaScript typically have datetime date () objects, arrays of Arra (), string strings.

The Date object is generally used to get and modify the current datetime, etc., we first declare the Date object, and then call the function in the form of some of the built-in functions in the Date object to get the dates and time.

Example: Var date=new date (); If you need to customize the initial value var date=new date (2012,10,1); Date can also be used (' Oct 1,2012 ') to define such a sub-canonical.

This is a common function used to set and get date time and so on.

We generally declare the Date object and then define the variable, which is used to hold the value returned after the call to the function in the Date object, and this allows the acquisition of the current datetime and so on, and the obtained value is stored in the variable.

Example: Var date=new date (); var mymonth=date.getmonth (); document.write (Mymonth);

This will print out the current month.

Get the week through Getday (), but get the week is returned 0 1 2 3 4 5 6; In turn, the corresponding Sunday to Saturday, so we want to return the value is Monday ... Sunday, you need to create an array,

The elements of this array are Monday to Sunday, var weekday=[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday], and then we will use the value returned by Getday () as the subscript for the array, so that we can complete the setting for the week.

Example: Var date=new date (); var weekday=[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]; My Mynum=date.getday ();

document.write ("Today is:" +weekday[mynum]);

Get the current time with gettime (); If you want to postpone the time back for as long as "+" to postpone one hours is 60*60*1000,1000 refers to milliseconds, the computer's computing time is milliseconds is not the second. In the same vein, the words are "-";

These are some of the uses of time objects. The usage of the string object is explained below.

We still need to store the string by declaring the string object, which is the direct assignment. var mystr= "I love JavaScript";

MyStr is a string object, we can get its length mystr.length;

You can convert the letters in the string to uppercase and lowercase, all to uppercase using toUpperCase () converted to lowercase or tolowercase ();

Example: Var mystr= "I love JavaScript";

Document.wirte (Mystr.touppercase ()); Print out all the letters of this sentence in uppercase.

document.write (Mystr.tolowercase ()); Print out all the letters of the sentence in lowercase.

We can also get the number of characters in the string var mystr= "I love JavaScript", or the MyStr string object as an example.

The syntax is the location of the string that you want to find within the Charat ();(). For example, find the first character in a string. Fill in 0; the index of the first string is 0 the last one is. length-1;

Mystr.charat (mystr.lenght-1); This way you can find out what the last character in the string is.

You can see what characters are in a known location, and you can find out what the first occurrence of a known character is.

The syntax is indexof (), there can be two elements, the first element is what you need to find the character, the second is in which position to start the search, the second can not fill.

Example: Var mystr= "I love JavaScript"; Mynum=mystr.indexof ("A", Mystr.indexof ("a") +1);

document.write ("Where the second a appears is:" +mynum); We are looking for a, we have to find the location is the first time to find the position of a +1 then find A is the second time a appears position.

Note that each of these spaces is also a character, if you do not find this character will be returned to the -1;indexof () can be filled with a string,

Split string syntax is split ();() inside can fill two elements the first is what to split, the second is divided into a few paragraphs, the second can not be written.

var mystr= "86-010-85468578"; var mynum=mystr.split ("-", 3); We'll separate the string of "-" into a "-" line. 3 means split three times.  If we don't fill in the first one, the string will separate each character. var mynum=mystr.split (""); The result is this 8,6,-, 0,1,0,-, 8,5,4,6,8,5,7,8;

If we add a number to the back 3 var mynum=mystr.split ("", 3); the result is 8,6,-; it's over.

Extracts a string.

The syntax is substring (); Inside or two elements the first element is the starting position of the extraction. The second one is the end position. The second if you do not fill in the words directly from the starting position to the last one of the string.

var mystr= "I love JavaScript"; var mynum=mystr.substring (0,6);   document.write (Mynum); The result is I love

And it is similar to the syntax of having one is substr (); It's also two elements. The first is still the starting position, and the second is the length of the extracted string.

var mystr= "I love JavaScript"; var mynum=mystr.substr (2,4);   document.write (Mynum); The result is love.

These are some of the uses of strings. The use of the math () method is explained below.

These are some of the properties of the Math object.  var L=math.pi; The value of L is pi.

These are some of the methods of the Math object.

var L=math.ceil (5.4); The value of L is 6 if I print L;

Some simple introduction to the math method

1:math.ceil (); Rounding up is a number entered in ceil () to take a value greater than or equal to his nearest integer.

2:math.floor (), rounding down, entering a number within floor (), and taking a nearest integer less than or equal to him.

3:math.round (); rounding; rounding the number of round inside to get an integer.

4:math.random (); random number; Take a random number between 0-1. If we need 0-100 of random numbers only need to be in math.random () *100; This random number has many digits after the decimal point and we don't need to just

You can convert this number to an integral type. var l=parseint ((Math.random ()) *10); In this way L is a random integer of 0-10.

These are some uses of the math object, and the following is a study of the use of an array object.

Define an array: var myarray=new array (); Defines an empty array of arrays whose name is myarray; You can enter a number in parentheses, which represents the length of the array.

Define the array's simultaneous assignment: Var myarray=[]; Fill in the elements of the array. MYARRAY[0]=XXX; Assigns a value to an array of 0 elements.

The methods of these array objects. Choose a few learning

1: Connection array concat ();

Connecting several arrays, Myarray1.concat (myarray2,myarray3,...., myarrayn), connecting Myarray1-n, and having no effect on the array itself. A new array needs to be defined to assign these concatenated arrays to it.

var myarray=myarray1.comcat (MYARRAY2,MYARRAY3);

We can also add parameters to the array.

var myarray=myarray1.concat ("L", 1); the "L" and the 1 connection arrays myarra1 inside.

In the connection array we can also join some elements between the array, this way we need to join ();

var myarray=myarray1.concat (myarray2). Join (""); each array in the connected array is separated by a space. Join () If nothing is filled in, the default is "," split

We want to think about what the divider does not add. Join (""); write double quotes within each array there is no delimiter.

2: Reverse array element order

Reverse (); Array.reverse (); the array is sorted upside down.

3: Selected Element

Slice (); There are two elements. The first is the starting position, the second is the terminating position, and the second one is automatically selected to the end of the array if it is not filled in. The values of these two elements represent the subscripts of the arrays. If the first number is entered in negative numbers. 1 that is, starting from the penultimate number, -2 is starting from the last second. This method also does not modify the array just to output a sub-array.

4: Array sorting

Sort (); Sort the sort () in an array to fill in the sorting function, if not fill in the default sort.

If you want to implement a flashback arrangement of numbers, we can first declare a function Sortnum (a, b) {return b-a;} and then call this function sort (sortnum) inside the sort ();

You can implement the flashback arrangement of array 1.

Built-in objects for JavaScript

Related Article

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.