Explanation of JavaScript built-in objects

Source: Internet
Author: User
Tags natural logarithm

Explanation of JavaScript built-in objects

 

Abstract: JavaScript is an object-oriented programming language (OOP ). The OOP language enables us to customize object and variable types. (In fact, it is only object-based and does not fully possess the three major features of object-oriented languages [encapsulation, inheritance, and polymorphism]). The advanced JavaScript section describes how to create your own objects. Now, we start to learn about built-in JavaScript objects and how to use them. Object is only a special type of data. Object

Has attributes and methods.

1. String object

A string is a basic data type of JavaScript. It should be noted that JavaScript strings are immutable, and the methods defined by the String class cannot change the content of strings. Image

A String. toUpperCase () method returns a brand new String instead of modifying the original String.

You can create a String object using the following methods:

Method 1: var str = new String (Hello World );

Method 2: var str = String (Hello World );

Method 3: var str = Hello World;

The String class defines a large number of methods to operate on strings. It is generally divided into the following types: Search for substrings, intercept, split and splice strings, match regular expressions, and change String styles.

String object attributes

Attribute Description
Constructor Reference to the function that creates the object
Length String Length
Prototype Allows you to add attributes and methods to an object

String object Method

Method Description
Anchor () Create an HTML anchor.
Big () Use a large font to display strings.
Blink () Displays a flashing string.
Bold () Use bold to display strings.
CharAt () Returns the characters at the specified position.
CharCodeAt () Returns the Unicode encoding of characters at the specified position.
Concat () The connection string.
Fixed () Displays strings in typewriter text.
Fontcolor () Use the specified color to display the string.
Fontsize () Use the specified size to display the string.
FromCharCode () Creates a string from the character encoding.
IndexOf () Returns a string.
Italics () Use italics to display strings.
LastIndexOf () Search for strings from the back and forward.
Link () Display the string as a link.
LocaleCompare () Compares two strings in a specific local order.
Match () Find the matching of one or more regular expressions.
Replace () Replace the substring that matches the regular expression.
Search () Returns the value that matches the regular expression.
Slice () Extract the string segment and return the extracted part in the new string.
Small () Use a small font size to display strings.
Split () Splits a string into a string array.
Strike () Use strikethrough to display strings.
Sub () Returns the string as a subscript.
Substr () Extracts a specified number of characters from the start index number.
Substring () Extract the characters between two specified index numbers in the string.
Sup () Displays the string as a superscript.
ToLocaleLowerCase () Converts a string to lowercase.
ToLocaleUpperCase () Converts a string to uppercase.
ToLowerCase () Converts a string to lowercase.
ToUpperCase () Converts a string to uppercase.
ToSource () The source code of the object.
ToString () Returns a string.
ValueOf () Returns the original value of a string object.
Ii. Date object

You can use these methods to create a date object.

Method 1: var d = new Date (ms); // ms indicates the number of milliseconds from a.m., January 1 ,.

Method 2: var d = new Date (year, month [, day, hour, minute, second, millisecond]);

Method 3: var d = new Date (localDateString); // This is not so common. The format is relatively common

If you want to create a date object for the current time. Use a constructor without parameters to directly create a new Date. Of course, we cannot ignore this new feature. The String mentioned above can be omitted, but we cannot do this here. Because Date () is a Date string returned by the toString of a Date object implemented by the browser. Therefore, the two cannot be mixed here.

The Date object is used to process the Date and time. The Date object automatically saves the current Date and time as its initial value.
Most methods of Date objects are as follows:
GetXXX: Get year, month, day, hour, minute, second, and so on.
SetXXX: Set year, month, day, hour, minute, second, and so on.

ToXXXString: convert to a string of a certain format.

Date Object Attributes

 

Attribute Description
Constructor Returns a reference to the Date function that creates this object.
Prototype Allows you to add attributes and methods to objects.

 

Date object Method

 

Method Description
Date () Returns the date and time of the current day.
GetDate () Returns a day of a month from the Date object (1 ~ 31 ).
GetDay () Returns a day of the week from the Date object (0 ~ 6 ).
GetMonth () Returns the month from the Date object (0 ~ 11 ).
GetFullYear () Returns the year from the Date object with four digits.
GetYear () Use the getFullYear () method instead.
GetHours () Returns the hour of the Date object (0 ~ 23 ).
GetMinutes () Returns the minute of the Date object (0 ~ 59 ).
GetSeconds () Returns the number of seconds of the Date object (0 ~ 59 ).
GetMilliseconds () Returns the millisecond of the Date object (0 ~ 999 ).
GetTime () Returns the number of milliseconds since January 1, January 1, 1970.
GetTimezoneOffset () Returns the minute difference between the local time and Greenwich Mean Time (GMT.
GetUTCDate () Returns the Day (1 ~ 31 ).
GetUTCDay () Returns the day of the week (0 ~ 6 ).
GetUTCMonth () Returns the month (0 ~ 11 ).
GetUTCFullYear () Returns the four-digit year from the Date object based on the universal time.
GetUTCHours () Returns the hour of the Date object based on the Universal Time (0 ~ 23 ).
GetUTCMinutes () Returns the minute of the Date object based on the Universal Time (0 ~ 59 ).
GetUTCSeconds () Returns the second of the Date object based on the Universal Time (0 ~ 59 ).
GetUTCMilliseconds () Returns the millisecond of the Date object based on the Universal Time (0 ~ 999 ).
Parse () Returns the number of milliseconds from midnight, January 1, January 1, 1970 to the specified date (string.
SetDate () Set a day of the month in the Date object (1 ~ 31 ).
SetMonth () Set the month (0 ~ 11 ).
SetFullYear () Set the year (four digits) in the Date object ).
SetYear () Use setFullYear () instead.
SetHours () Set the hour in the Date object (0 ~ 23 ).
SetMinutes () Set the minute in the Date object (0 ~ 59 ).
SetSeconds () Set the second in the Date object (0 ~ 59 ).
SetMilliseconds () Set the millisecond in the Date object (0 ~ 999 ).
SetTime () Set the Date object in milliseconds.
SetUTCDate () Set the day of the month in the Date object according to the Universal Time (1 ~ 31 ).
SetUTCMonth () Set the month in the Date object according to the Universal Time (0 ~ 11 ).
SetUTCFullYear () Set the year (four digits) in the Date object according to the Universal Time ).
SetUTCHours () Set the hour in the Date object according to the Universal Time (0 ~ 23 ).
SetUTCMinutes () Set the minute in the Date object according to the Universal Time (0 ~ 59 ).
SetUTCSeconds () Set the second in the Date object according to the Universal Time (0 ~ 59 ).
SetUTCMilliseconds () Set the millisecond in the Date object according to the Universal Time (0 ~ 999 ).
ToSource () Returns the source code of the object.
ToString () Converts a Date object to a string.
ToTimeString () Converts the time part of the Date object to a string.
ToDateString () Converts the Date part of the Date object to a string.
ToGMTString () Use the toUTCString () method instead.
ToUTCString () Converts a Date object to a string based on the universal time.
ToLocaleString () Converts a Date object to a string based on the local time format.
ToLocaleTimeString () Converts the time part of the Date object to a string based on the local time format.
ToLocaleDateString () Converts the Date part of the Date object to a string based on the local time format.
UTC () Returns the number of milliseconds from January 1, January 1, 1970 to the specified date based on the universal time.
ValueOf () Returns the original value of the Date object.

 

 

3. Array objects

An array object uses a separate variable name to store a series of values. The common attribute of an array is length, which represents the number of elements in the array. Commonly used methods of arrays include sorting, adding, and deleting elements,

Concatenate another array and convert it into a string. The methods for adding and removing elements also simulate the data structure of stacks or queues.

Let's take a look at the creation of an Array object.

First: var arr = new Array (10 );

This method is not very practical in actual use. Unlike many compilation languages, the js array length is variable, which not only enhances flexibility, we also have a better choice.

Var arr = new Array (one, two, three );

The new method is usually used to create arrays. You can also use new Array () to create an empty Array object. In general, I recommend the following methods:

Third: var arr = [one, two, three];

Creating an array object using the array literal is not only simple and easy to read, but also almost equivalent to creating an array object using the new method. Array Object Attributes
Attribute Description
Constructor Returns a reference to the array function that creates this object.
Length Sets or returns the number of elements in the array.
Prototype Allows you to add attributes and methods to objects.


Array object Method
Method Description
Concat () Concatenate two or more arrays and return results.
Join () Put all elements of the array into a string. The elements are separated by the specified delimiter.
Pop () Delete and return the last element of the array
Push () Add one or more elements to the end of the array and return a new length.
Reverse () The order of elements in the array is reversed.
Shift () Delete and return the first element of the array
Slice () Returns the selected element from an existing array.
Sort () Sorts elements of an array.
Splice () Delete elements and add new elements to the array.
ToSource () Returns the source code of the object.
ToString () Converts an array to a string and returns the result.
ToLocaleString () Converts an array to a local array and returns the result.
Unshift () Add one or more elements to the beginning of the array and return a new length.
ValueOf () Returns the original value of the array object.

4. Boolean (logical) object

A Boolean (logical) object is used to convert non-logical values to logical values (true or false ).

Syntax for creating a Boolean object:
New Boolean (value); // Constructor
Boolean (value); // Conversion Function

In JavaScript, a Boolean value is a basic data type. A Boolean object is a Boolean object that packs a Boolean value. A Boolean object is mainly used to convert a Boolean value to a string's toString () method. When the toString () method is called to convert a Boolean value to a string (usually called implicitly by JavaScript), JavaScript internally converts the Boolean value to a temporary Boolean object, then, call the toString () method of this object.

Boolean Object Attributes

 

Attribute Description
Constructor Returns a reference to the Boolean function that creates this object.
Prototype Allows you to add attributes and methods to objects.

 

Boolean object Method

Method Description
ToSource () Returns the source code of the object.
ToString () Converts a logical value to a string and returns the result.
ValueOf () Returns the original value of a Boolean object.
5. The use of the Math class of JavaScript Math (arithmetic) object is relatively narrow, because Math is a class of mathematical computing rather than a Data Structure class, but we also see Math. random and a variety of common methods such as integer. Math is usually a "static" class, because no one will instantiate a Math object, but directly use its "static" method. Some materials are called Math objects directly, here we may call it a "static" class.

First, I must introduce the random method, because it is often used and useful. It is always indispensable to create random events. It is also useful to prevent caching. Math. the Return Value of the random method is an open floating point number (0, 1) between 0 and 1. It is very simple to use. The only thing to note is that, we need to be cautious when filtering floor and ceil methods in order to perform an integer. The former indirectly converts random to the open interval after the anterior closure, while the latter is the closed interval after the former. If we need a random number ranging from 1 to, there are two common solutions:

Method 1: Math. ceil (Math. random * 100 );

Method 2: Math. floor (Math. random * 100) + 1;

The ceil method and the floor method are both mathematical methods used to get an integer. According to the meaning of a word, we can understand that the former is rounded up, while the latter is rounded down. When we randomly select an array element from a continuous array object, we can easily choose [ipad, iphone, ipod touch, ipod nano, macbook] [Math. ceil (Math. random () * 4)]; when we want to get close to homes, we can use Math. round method, which is used to obtain the rounded value according to the number. For example, Math. round (5.4) returns 5. If Math. round (5.5) is used, the answer is 6 instead of 5.


Math Object Attributes

 

Attribute Description
E Returns the base number of the natural logarithm (approximately 2.718) of the arithmetic constant e ).
LN2 Returns the natural logarithm of 2 (approximately 0.693 ).
LN10 Returns the natural logarithm of 10 (approximately 2.302 ).
LOG2E Returns the base-2 logarithm of e (approximately 1.414 ).
LOG10E Returns the base-10 logarithm of e (approximately 0.434 ).
PI Returns the circumference rate (approximately 3.14159 ).
SQRT1_2 Returns the reciprocal of the square root of 2 (approximately 0.707 ).
SQRT2 Returns the square root of 2 (approximately 1.414 ).

 

Math object Method

 

Method Description
Abs (x) Returns the absolute value of a number.
Acos (x) Returns the arc cosine of a number.
Asin (x) Returns the arc sine of the number.
Atan (x) Returns the arc tangent value of x with a value between-PI/2 and PI/2 radian.
Atan2 (y, x) Returns the angle from the x-axis to the point (x, y) (between-PI/2 and PI/2 radian ).
Ceil (x) The logarithm is rounded up.
Cos (x) Returns the cosine of the number.
Exp (x) Returns the index of e.
Floor (x) The logarithm is deprecated.
Log (x) Returns the natural logarithm of the number (Base 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 Power y of x.
Random () Returns 0 ~ A random number between 1.
Round (x) Round the number to the nearest integer.
Sin (x) Returns the sine of the number.
Sqrt (x) Returns the square root of a number.
Tan (x) Returns the tangent of the angle.
ToSource () Returns the source code of the object.
ValueOf () Returns the original value of the Math object.

 

6. What is a RegExp object?

RegExp is the abbreviation of a regular expression. When you retrieve a text, you can use a mode to describe the content to be retrieved. RegExp is in this mode. A simple mode can be a single character. More complex modes include more characters and can be used for parsing, format check, replacement, and so on. You can specify the search position in the string and the type of characters to be retrieved.
VII. JavaScript Global object this is an inherent object, with the aim of putting all Global methods in one object. The Global object cannot be created using the new operator. It is created when the Scripting engine is initialized and made available even if its method and attributes are available.

 

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.