JavaScript Series three: JavaScript Built-in objects

Source: Internet
Author: User
Tags array definition base 10 logarithm natural logarithm set time square root

1. What is an object
All things in JavaScript are objects, such as strings, numbers, arrays, functions, and so on, each with properties and methods.
Object properties: Reflect the object of certain specific properties, such as: the length of the string, the image of the long width, etc.;
Object: An action that can be performed on an object. For example, the form "submit" (submit), the time of "get" (getYear), etc.;
JavaScript provides a number of built-in objects, such as String, Date, array, and so on, defined before using the object, using the array object as follows:
Syntax for accessing object properties:
Objectname.propertyname
Methods for accessing objects:
Objectname.methodname ()

2. Date Object
Date objects can store any date and can be accurate to milliseconds (1/1000 seconds).
Defining a Time Object
var udate=new Date ();
Note: Use the first letter of the keyword New,date () must be capitalized.
Makes udate a Date object and has an initial value: The current time (current computer system time).
Custom Initial values:
var d = new Date (2012, 10, 1); October 1, 2012
var d = new Date (' OCT 1, 2012 '); October 1, 2012
We'd better use the "method" described below to strictly define the time.
Access method Syntax: "< date objects >.< methods >"
Common methods for processing time and dates in a Date object:
Get/setdate () return/Set Date
Get/setfullyear () Returns/sets the year, expressed in four-digit numbers
Get/setyear () return/set year
Get/setmonth () return/Set month 0: January 112 months
Get/setday () return/set week, 0 week (s)
Get/sethours () return/Set hour, 24-hour system
Get/setminutes () Returns/sets the number of minutes
Get/setseconds () Returns/sets the number of seconds
Get/settime () return/Set time (in milliseconds)

3. Return/Set Year method
Get/setfullyear () Returns/sets the year, expressed as a four-digit number.
Note: Different browsers, Mydate.setfullyear (81) have different results, and the year is set to 0081 or 812 cases.
Attention:
1. The result format is: week, month, day, year, time, minute, second, time zone. (Firefox browser)
2. Different browser, time format has difference.

4. Return to Week method
GetDay () returns a number of 0-6 in the week, 0 for Sunday.

5. Return/Set Time method
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.
Note: 1. One hour, 60 minutes, 60 seconds, 1000 milliseconds a second.
2. The time delay is 1 hours, that is: "X.settime (X.gettime () + 60 * 60 * 1000);"

6. String Object
The properties of the Access string object length:
Methods for accessing String objects:
Use the toUpperCase () method of the string object to convert the string lowercase letters to uppercase
Use the toLowerCase () method to turn all uppercase letters of a string into lowercase strings

7. Returns the character at the specified position
The CharAt () method returns the character at the specified position. The returned character is a string of length 1.
Stringobject.charat (Index)
Parameter description:
Index: Required, a number that represents a position in a string, which is the subscript of a character in a string
Attention:
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.
Note: A space is also counted as a character.

8. Returns the location of the first occurrence of the specified string
The IndexOf () method returns the position of the first occurrence of a specified string value in a string.
Stringobject.indexof (substring, startpos)
Description
1. The method will retrieve the string stringobject from beginning to end to see if it contains substring substring.
2. Optional parameters, starting from the startpos position of the stringobject to find substring, if no this parameter will be found from the beginning of the Stringobject.
3. If a substring is found, the position of the first occurrence of the substring is returned. The character position in Stringobject is starting at 0.
Attention:
The 1.indexOf () method is case-sensitive.
2. If the string value that you want to retrieve does not appear, the method returns-1.

9, String split ()
The split () method splits a string into an array of strings and returns this array.
Stringobject.split (Separator,limit)
Parameter description:
Separator: Required. Split Stringobject from the location specified by this parameter
Limit: Optional parameter, the number of splits, such as setting this parameter, the returned substring will not be extra for this parameter specified by the array, if none of this parameter is an unlimited number of times.
Note: If you use an empty string ("") as a separator, then each character in the Stringobject will be split.
The string will be split in different ways:
Splitting a string with a specified symbol
var mystr = "www.imooc.com";
document.write (Mystr.split (".") + "<br>");
document.write (Mystr.split (".", 2) + "<br>");
To split a string into characters
document.write (Mystr.split ("") + "<br>");
document.write (Mystr.split ("", 5));

10. Extracting string substring ()
The substring () method is used to extract the character of a string intermediary between two specified subscripts.
Stringobject.substring (Starpos,stoppos)
Parameter description:
Startpos: Required, a non-negative integer, starting position
Stoppos: Optional, a non-negative integer, ending, if omitted, the returned substring continues until the end of the string object
Attention:
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.

11. Extract the specified number of characters substr ()
The substr () method extracts a specified number of strings from the string that start at the startpos position.
Stringobject.substr (Startpos,length)
Parameter description:
Startpos: Required, the starting position of the substring to extract, must be a numeric value
Length: Optional, the character of the string to be extracted, if omitted, to return the Stringobject start Startpos to the end of the Stringobject
Attention:
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.
If Startpos is negative and the absolute value is greater than the string length, Startpos is 0.

12. Math Object
The Math object, which provides a mathematical calculation of the data.
Note: The Math object is an intrinsic object that you can call all of its properties and methods without having to create it, directly using math as an object. This is the difference between it and the Date,string object.
Math Object Properties
e returns the arithmetic constant E, which is the base of the natural logarithm (approximately equal to 2.718)
LN2 returns the natural logarithm of 2 (approximately equal to 0.693)
LN10 returns the natural logarithm of 10 (approximately equal to 2.302)
LOG2E returns the logarithm of E with a base of 2 (approximately equal to 1.442)
LOG10E returns the base 10 logarithm (approximately equal to 0.434)
Pi returns PI (approximately equal to 3.14.159)
Sort1_2 returns the reciprocal of the square root of 2 (approximately equal to 0.707)
SORT2 returns the square root of 2 1.414
Math Object method
ABS (x) absolute value of the return number
ACOs (x) inverse cosine of the return number
ASIN (x) inverse sine value of the return number
Atan (x) returns the inverse tangent value of a number
ATAN2 (y,x) returns the angle (in radians) of the x-axis to the point
Ceil (x) rounding on a logarithmic
COS (x) The cosine of the return number
EXP (x) returns the exponent of E
Floor (x) rounding down logarithm
Log (x) The natural logarithm of the number of returns (Bottom is E)
Max (x, y) returns the highest value in X and Y
Min (x, y) returns the lowest value in X and Y
Pow (x, y) returns the Y power of X
Random () returns the number of 0~1 between
Round (x) rounding the number to the nearest integer
Sin (x) the sine of the number of returns
sqrt (x) The square root of the number of returns
Tan (x) returns the tangent of the angle
Tosource (x) returns the source code of the object
VALUEOF (x) returns the original value of the Math object

13. Take the whole ceil upward ()
The Ceil () method can be rounded up to a number.
Math.ceil (x)
Description
x required, must be numeric
Note: It returns the integer that is greater than or equal to x and closest to X.

14. Take down the floor ()
The floor () method allows a number to be rounded down.
Math.floor (x)
Description
x required, any numeric or expression
Note: Returns the integer that is less than or equal to x and closest to X.

15. Rounding Round ()
The round () method rounds a number to the nearest integer.
Math.Round (x)
Description
x required, must be a number
Attention:
1. Returns the integer closest to X.
2. For 0.5, the method will be rounded up. (5.5 will be rounded to 6)
3. If x is similar to an integer on both sides, the result is close to the numeric value in the +∞ direction.

16. Random Number ()
The random () method returns a random number between 0 and 1 (greater than or equal to 0 but less than 1).
Math.random ();
Note: Returns a numeric value that is greater than or equal to 0 but less than 1 of the symbol is positive.
Note: Because it is a random number, the result is different for each run, but a value of 0 ~ 1.

17. Array Object
An array object is a collection of objects that can be of different types. Each member object of an array has a "subscript" that represents its position in the array, which is zero-based
Method of array Definition:
1. Defines an empty array:
var array name = new Array ();
2. Define an array of n empty elements when specified:
var array name =new array (n);
3. When defining an array, initialize the data directly:
var array name = [< element 1>, < element 2>, < element 3>...];
Array elements are used:
Array name [subscript] = value;
Note: The subscript of the array is enclosed in square brackets, starting with 0.
Array properties:
Length usage:< Array Object >.length; return: The length of the array, which is the number of elements in the array. It equals the subscript of the last element in the array plus one.
Array method:
Concat (): Joins two or more arrays and returns the result
Join (): Places all elements of an array into a string, and the elements are split by the specified delimiter
Pop (): Delete and return the last element of the array
Push (): Adds one or more elements to the end of the array and returns the new length
Reverse () Reverses the order of the elements in the array
Shift (): Delete and return the first element of the array
Slice (): Returns the selected element from an existing array
Sort (): Sorts the elements of an array
Splice (): Delete the element and add a new element to the array
Tosource (): Returns the source code of the object
ToString (): Converts the array to a string and returns the result
Tolocatestring (): Converts the array to a local array and returns the result
Unshift (): Adds one or more elements to the beginning of the array and returns the new length
ValueOf () returns the original value of the array object

18. Array Connection concat ()
The Concat () method is used to concatenate two or more arrays. This method returns a new array without changing the original array
Arrayobject.concat (array1,array2,..., Arrayn)
Description
Array1 the first array to concatenate
Arrayn Nth Number of groups
Note: This method does not alter an existing array, but simply returns a copy of the concatenated array.

19. Specify delimiter to concatenate array element join ()
The join () method is used to put all the elements in an array into a string. The elements are delimited by the specified delimiter.
Arrayobject.join (delimiter)
Description
Separator is optional. Specifies the delimiter to use. If this argument is omitted, a comma is used as the delimiter
Note: Returns a string that strings the elements in the array, with the < delimiter > placed between elements and elements. This method does not affect the original contents of the array

20. Reverse array element order reverse ()
The reverse () method reverses the order of the elements in the array.
Arrayobject.reverse ()
Note: This method changes the original array without creating a new array.

21. Selected element Slice ()
The slice () method returns the selected element from an existing array.
Arrayobject.slice (Start,end)
Description
Start: Must, specify where to start the selection, if it is negative, then it specifies the position from the end of the array,
End: Optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment.
1. Returns a new array containing the elements from start to end (excluding the element) in the Arrayobject.
2. The method does not modify the array, but rather returns a sub-array.
Attention:
1. Negative values can be used to select elements from the tail of the array.
2. If end is not specified, then the slice () method selects all elements from start to the end of the array.
3. String.slice () is similar to Array.slice ().

22. Sort Array ()
The sort () method causes the elements in the array to be arranged in a certain order.
Arrayobject.sort (method function)
Method functions: Optional, specify sort order, must be function
1. If the < method function is not specified, it is sorted in Unicode code order.
2. If you specify the < method function, then sort by the < method function > The sorting method specified.
Myarray.sort (SortMethod);

JavaScript Series three: JavaScript Built-in objects

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.