JavaScript built-in objects (1)

Source: Internet
Author: User
Tags set time string indexof

All things in JS are objects, such as strings, numbers, arrays, functions, and so on, each with properties and methods.

Object : Reflects the object's specific properties , such as the length of the string, the width of the image, and so on.

object: an action that can be performed on an object. For example, the form's "submit" (submit), Time "get" (getYear), and so on.

Syntax for accessing object properties:


Use the Length property of the array object to get the lengths of the arrays.

1 var myarray=New Array (6); // defining an Array object 2 var myl=myarray.length; // To access the length of array lengths property

methods for accessing objects:

Objectname.methodname (); //Back "()" is important, can't forget

Use the toUpperCase () method of the string object to convert the text to uppercase:

1 var mystr= "Hello world!"; // Create a string 2 var // using String Object methods
One, date dates object

Define a Date object:

var udate=new Date ()

Note : Use the first letter of the keyword New,date () to capitalize. Makes udate a Date object and has an initial value: The current time (current computer system time).

Customize the initial value of the Date object by doing the following:

1 var New Date (1);  // October 1, 2012 2 var New // October 1, 2012

A more canonical method of defining time, such as:< Date Object >.< method >.

Common methods for processing time and dates in a Date object:

1. Return/Set Year method

format as;

Get/setfullyear ()

The returned result is represented by a four-digit number, and the attention is strictly case sensitive.


1 var mydate=New Date (); // Current Time March 6, 2014 2 document.write (mydate+ "<br>"); // Output Current Time 3 document.write (mydate.getfullyear () + "<br>"); // Output Current year 4 // Set Year 5 // The output year is set to 0081 years. 

Input Result:

Thu 10:57:47 gmt+08002014thu Mar 0081 10:57:47 gmt+0800

The result format is: week, month, day, year, time, minute, second, time zone. (Firefox browser)

2. Return to Week method GetDay ()

GetDay () returns a number of 0-6 in the week, 0 for Sunday. If you want to return the corresponding "week", complete with the array, the code is as follows:

1<script type= "Text/javascript" >2   varMydate=NewDate ();//Defining Date Objects3   varweekday=["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];4 //define array objects, assign values to each array item5   varMynum=mydate.getday ();//The return value is stored in the variable mynum6document.write (Mydate.getday ());//output getday () gets the value7document.write ("Today is:" + weekday[mynum]);//output Day of the week8</script>

Input Result:

5 Today is: Friday
3. Return/Set Time method Get/settime ()

get/settime () Returns/sets the time, in milliseconds, to calculate the number of milliseconds from January 1, 1970 0 o'clock to the date the Date object refers to.

Defer the current date object's time by 1 hours, as shown in the following code:

1 <script type= "Text/javascript" >2   var mydate=New  Date (); 3   document.write ("Current Time:" +mydate+ "<br>"); 4   Mydate.settime (Mydate.gettime () + * + *);  An hour 60 minutes, a minute 60 seconds, a second 1000 milliseconds  5   document.write ("Delay One hour:" + mydate); 6 </script>

Two, string object strings

Method of defining a string: Direct assignment, such as;

var mystr = "I Love javascript!"

  The properties of the Access string object length:



1 var mystr= "Hello world!" ; 2 var myl=mystr.length;

The return value will be numeric.

methods for accessing string objects:

Stringobject.method ()


1 var mystr= "Hello world!" ; 2 var mynum=mystr.touppercase (); //"touppercase" converts lowercase letters to uppercase with "toLowerCase ()"
1. Returns the character of the specified position charAt ()

The CharAt () method returns the character at the specified position. The returned character is a string of length 1 .


Stringobject.charat (Index)

Description: "Index" is required. A number that represents a position in a string, which is the subscript of a character in a string.

Note: 1. The subscript for the first character in a string is 0. The subscript for the last character is the string length minus one (string.length-1).

2. If the parameter index is not between 0 and String.length-1, the method returns an empty string.


1 <script type= "Text/javascript" >2   var mystr= "I Love javascript!" 3   document.write (Mystr.charat (2)); 4 </script>

Output Result:

2. Returns the position of the first occurrence of the specified string indexOf ()


Stringobject.indexof (substring, startpos)

Description: Substring required. The string value that needs to be retrieved. Startpos Optional. Specifies where to start the search in the string. If this argument is omitted, the search begins at the first character of the string.


1 <script type= "Text/javascript" >2   var str= "I Love javascript!" 3   document.write (Str.indexof ("I") + "<br/>"); 4   document.write (Str.indexof ("V") + "<br/>"); 5   document.write (Str.indexof ("V", 8)); 6 </script>

Output Result:

3. String Split Split ()

The split () method splits a string into an array of strings and returns this array.


Stringobject.split (Separator,limit)

Description: Separator required. Dividing a stringobject from a specified place. Limit is optional. The number of splits.

Note: If you use an empty string ("") as a separator, then each character in the Stringobject will be split.

Example 1:

1 var mystr = ""; 2 document.write (Mystr.split (".") + "<br>"); 3 document.write (Mystr.split (".", 2) + "<br>");

Output Result:


Example 2:

1 document.write (Mystr.split ("") + "<br>"); 2 document.write (Mystr.split ("", 5));

Output Result:

W,w,w,., i,m,o,o,c,., c,o,mw,w,w,., I
4. Extracting string substring ()

The substring () method is used to extract the character of a string intermediary between two specified subscripts.


Stringobject.substring (Starpos,stoppos)

Description: Startpos required. Start position. Stoppos is optional. Ending position, such as omitting the argument, returns the substring until the end of the string object.


1. The returned content is all characters from start (the character containing the start position) to Stop-1, whose length is stop minus start.

2. If the parameter start is equal to stop, then the method returns an empty string (that is, a string of length 0).

3. If start is larger than stop, the method will exchange the two parameters before extracting the substring.


1 <script type= "Text/javascript" >2   var mystr= "I love JavaScript"; 3   document.write (mystr.substring (7)); 4   document.write (mystr.substring (2,6)); 5 </script>

Output Result:

5. Extract the specified number of characters substr ()


Stringobject.substr (Startpos,length)

Description: Length is optional. Extracts the length of the string, if omitted, returns the character from the beginning of the stringobject to the end.

Note: If the parameter startpos is a negative number, the position is calculated from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on.


1 <script type= "Text/javascript" >2   var mystr= "I Love javascript!" ; 3   document.write (MYSTR.SUBSTR (7)); 4   document.write (Mystr.substr (2,4)); 5 </script>

Output Result:


  To be Continued ...

This article refers to the MU class network

JavaScript built-in objects (1)

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