Common JavaScript objects and javascript objects

Source: Internet
Author: User
Tags natural logarithm

Common JavaScript objects and javascript objects


Common objects, attributes, and usage:

(1). Array object

Array objects are used to store multiple values in a single variable.
Syntax for creating an Array object:

New Array ();
New Array (size );
New Array (element0, element1,..., elementn );

Parameters

The parameter size is the expected number of array elements. The returned array. The length field is set to the value of size. Element..., elementn is the parameter list. When these parameters are used to call the constructor Array (), the elements of the newly created Array are initialized to these values. Its length field is also set to the number of parameters.


Return Value

Returns the newly created and initialized array. If no parameter is used when the constructor Array () is called, the returned Array is empty and the length field is 0. When a constructor is called, only one numeric parameter is passed. The constructor returns an array with a specified number and an undefined element. When other parameters call Array (), the constructor initializes the Array with the value specified by the parameter. When a constructor is called as a function without the new operator, its behavior is exactly the same as that when the new operator is used to call it.


Array Object Attributes
Attribute description
Constructor returns a reference to the array function that creates this object.
Length setting or return the number of elements in the array.
Prototype enables you to add attributes and methods to an object.

Array object Method
Method description
Concat () connects two or more arrays and returns results.
Join () puts all elements of the array into a string. The element is entered by the specified separator

Separate rows.
Pop () deletes and returns the last element of the array.
Push () adds one or more elements to the end of the array and returns a new length.
The order of elements in the array is reversed by reverse.
Shift () deletes and returns the first element of the array.
Slice () returns the selected element from an existing array
Sort () sorts array elements
Splice () deletes an element and adds a new element to the array.
ToSource () returns the source code of the object.
ToString () converts an array to a string and returns the result.
ToLocaleString () 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 a new length.

.
ValueOf () returns the original value of the array object


Example:

Var arr = new Array (3 );
Arr [0] = "hello ";
Arr [1] = 3.245;
Arr [2] = "";
Document. write (arr + '<br/>'); // hello, world, hahaha
Document. write (arr. concat ('20140901') + '<br/> ');
Document. write (arr. join () + '<br/> ');
Document. write (arr. pop () + '<br/> ');
Document. write (arr. shift () + '<br/> ');
Document. write (arr. unshift () + '<br/> ');
Document. write (arr. push ("hi") + '<br/>'); // 4
Document. write (arr. reverse () + '<br/> ');
Document. write (arr. sort ());//


/* Running result:

Hello, 3.245,
Hello, 3.245, a, 123
Hello, 3.245,
A
Hello
1
2
Hi, 3.245
3.245, hi

*/


(2). Date object

The Date object is used to process the Date and time.
Syntax for creating a Date object:

Var myDate = new Date ()

Note: The Date object automatically saves the current Date and time as its initial value.
Date Object Attributes
Attribute description
Constructor returns a reference to the Date function that creates this object.
Prototype enables you to add attributes and methods to an object.
Date object Method
Method description
Date () returns the Date and time of the current day.
GetDate () returns a day (1 ~ 31 ).
GetDay () returns a day of a 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 in four digits.
Use the getFullYear () method instead of getYear.
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 (0 ~) of the Date object ~ 999 ).
GetTime () returns the number of milliseconds since January 1, January 1, 1970.
GetTimezoneOffset () returns the value

Minute difference.
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 (0 ~ 999)

.
Parse () returns the millisecond from midnight, January 1, January 1, 1970 to the specified date (string ).

Number.
SetDate () sets a day of the month in the Date object (1 ~ 31 ).
SetMonth () sets the month (0 ~) in the Date object ~ 11 ).
SetFullYear () sets the year (four digits) in the Date object ).
Use the setFullYear () method instead of setYear.
SetHours () sets the hour (0 ~) in the Date object ~ 23 ).
SetMinutes () sets the minute (0 ~ 59 ).
SetSeconds () sets the second (0 ~) in the Date object ~ 59 ).
SetMilliseconds () sets the millisecond (0 ~) in the Date object ~ 999 ).
SetTime () sets the Date object in milliseconds.
SetUTCDate () sets the day of the month in the Date object based on the Universal Time (1 ~ 31 ).
SetUTCMonth () sets the month (0 ~ 11 ).
SetUTCFullYear () sets the year (four digits) in the Date object according to the Universal Time.

Number ).
SetUTCHours () sets the hour (0 ~ 23 ).
SetUTCMinutes () sets the minute (0 ~

59 ).
SetUTCSeconds () sets the second (0 ~) in the Date object based on the universal time ~

59 ).
SetUTCMilliseconds () sets the millisecond (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.
Use the toUTCString () method instead of toGMTString.
ToUTCString () converts a Date object to a string based on the universal time.
ToLocaleString () converts a Date object to a word based on the local time format

String.
ToLocaleTimeString () according to the local time format, the time part of the Date object

Convert to string.
ToLocaleDateString () according to the local time format, the Date part of the Date object

Convert to string.
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.


Example:

Var dt = new Date ();
Document. write (dt. getFullYear () + '<br/> ');
Document. write (dt. getYear () + '<br/> ');
Document. write (dt. getDate () + '<br/> ');
Document. write (dt. getMonth () + '<br/> ');
Document. write (dt. getDay () + '<br/> ');
Document. write (dt. getMinutes () + '<br/> ');
Document. write (dt. getHours () + '<br/> ');
Document. write (dt. getSeconds () + '<br/> ');
Document. write (dt. getMilliseconds () + '<br/> ');
Document. write (dt. toDateString () + '<br/>'); // Sun Jul 19 2015.
Document. write (Date () + '<br/>'); // Sun Jul 19 2015 18:30:55 GMT + 0800 (China Standard Time)


/* Running result:

2015
115
29
6
3
13
17
29
313
Wed Jul 29, 2015
Wed Jul 29 2015 17:13:29 GMT + 0800

*/


(3). String object

String object is used to process text (String ).
Syntax for creating a String object:

New String (s );
String (s );

Parameters

The parameter s is the value to be stored in the String object or converted to the original String.


Return Value

When String () and the new operator are used as constructors, it returns a newly created String object, which stores the String representation of String s or s. When you do not use the new operator to call String (), it only converts s to the original String and returns the converted value.


String object attributes
Attribute description
Constructor references the function used to create the object
Length String length
Prototype allows you to add attributes and methods to an object
String object Method
Method description
Anchor () creates an HTML anchor.
Big () displays strings in a large font.
Blink () displays the flashing string.
Bold () displays strings in bold.
CharAt () returns the character at the specified position.
CharCodeAt () returns the Unicode encoding of characters at the specified position.
Concat () connection string.
Fixed () displays strings in typewriter text.
Fontcolor () uses the specified color to display strings.
Fontsize () uses the specified size to display the string.
FromCharCode () creates a string from the character encoding.
IndexOf () to retrieve strings.
Italics () uses italics to display strings.
LastIndexOf () searches for strings from the back and forward.
Link () displays the string as a link.
LocaleCompare () compares two strings in a specific local order.
Match () finds matching of one or more regular expressions.
Replace () replaces the substring that matches the regular expression.
Search () searches for values that match regular expressions.
Slice () extracts string fragments and returns the extracted

.
Small () uses a small font size to display strings.
Split () splits the string into a string array.
Strike () uses strikethrough to display strings.
Sub () displays the string as a subscript.
Substr () extracts a specified number of characters from the start index number.
Substring () is used to extract the characters between two specified index numbers.
Sup () displays the string as a superscript.
ToLocaleLowerCase () converts the string to lowercase.
ToLocaleUpperCase () converts the string to uppercase.
ToLowerCase () converts a string to lowercase.
ToUpperCase () converts a string to uppercase.
ToSource () indicates the source code of the object.
ToString () returns a string.
ValueOf () returns the original value of a string object.
String object description

A string is a basic data type of JavaScript. The length attribute of the String object declares the number of characters in the String. The String class defines a large number of String operations, such as extracting characters or substrings from a String, or retrieving characters or substrings. It should be noted that JavaScript strings are immutable, and the methods defined by the String class cannot change the content of strings. A method like String. toUpperCase () returns a brand new String instead of modifying the original String. In the earlier JavaScript Implementation of Netscape code base (such as Firefox implementation), the string behavior is like a read-only character array. For example, to extract the third character from string s, use s [2] instead of s. charAt (2 ). In addition, when a for/in loop is applied to a string, it will enumerate the array subscript of each character in the string (but note that the length attribute cannot be enumerated according to ECMAScript standards ). Because the string array behavior is not standard, avoid using it.


Example:
Var str = 'helloworld ';
/* Alert (str. indexOf ('hes')> = 0 )? 'Find ': 'Not find ');*/
Document. write (str. indexOf ('mm') + '<br/> ');
Document. write (str. fontcolor ("blue") + '<br/> ');
Document. write (str. strike () + '<br/>'); // display the string with the delete symbol
Document. write (str. concat ("hi") + '<br/> ');
Document. write (str. toUpperCase () + '<br/> ');
Document. write (str. toLowerCase () + '<br/> ');
Document. write (str. replace ('hes', 'nihao') + '<br/> ');

/* Running result:

-1
Helloworld
Helloworld
Helloworldhi
HELLOWORLD
Helloworld
Nihaolloworld */


(4). Number object

The Number object is the packaging object of the original value.
Syntax for creating a Number object:

Var myNum = new Number (value );
Var myNum = Number (value );

Parameters

The parameter value is the value of the Number object to be created, or the value to be converted to a Number.

Return Value

When Number () and operator new are used as constructors, it returns a newly created Number object. If the new operator is not used and Number () is called as a function, it will convert its own parameter into an original value and return this value (if the conversion fails, returns NaN ).


Number Object Attributes

Attribute description
Constructor returns a reference to the Number function that creates this object.
MAX_VALUE indicates the maximum number.
MIN_VALUE represents the smallest number.
NaN is a non-numeric value.
NEGATIVE_INFINITY is negative infinity. This value is returned when overflow occurs.
POSITIVE_INFINITY is positive infinity. This value is returned when overflow occurs.
Prototype enables you to add attributes and methods to an object.
Number object Method
Method description
ToString converts a number to a string and uses the specified base number.
ToLocaleString converts a number to a string in the local numeric format order.
ToFixed converts a number to a string. The result has a specified number of digits after the decimal point.

Number.
ToExponential converts the object value into an exponential notation.
ToPrecision format the number to the specified length.
ValueOf returns the basic numeric value of A Number object.
Number Object Description

In JavaScript, numbers are a basic data type. JavaScript also supports the Number object, which is the packaging object of the original value. When necessary, JavaScript will automatically convert between the original data and the object. In JavaScript 1.1, you can use the constructor Number () to explicitly create a Number object, although this is not necessary. The constructor Number () can be used directly as a conversion function instead of the new operator. When you call Number () in this way, it will convert its parameter into a Number, and then return the converted original value (or NaN ). Constructors are usually used as placeholders for five useful numeric constants, these five useful numeric constants are the maximum number that can be expressed, the minimum number that can be expressed, positive infinity, negative infinity, and special NaN values. Note that these values are the attributes of the constructor Number (), rather than the attributes of a single Number object.


For example, the MAX_VALUE attribute is correct:

Var big = Number. MAX_VALUE

However, this is incorrect:

Var n = new Number (2 );
Var big = n. MAX_VALUE

For comparison, let's take a look at other methods of toString () and Number objects. They are the methods of each Number object, not the methods of Number () constructor. As mentioned above, when necessary, JavaScript will automatically convert the original value into a Number object, and call the Number method can be either a Number object or a raw Number value.

Var n = 123;
Var binary_value = n. toString (2 );


(5). Boolean object

Boolean indicates two values: "true" or "false ".
Syntax for creating a Boolean object:

New Boolean (value); // Constructor
Boolean (value); // Conversion Function

Parameters

The parameter value is the value stored by a Boolean object or converted to a Boolean value.

Return Value


When called as a constructor (with the new operator), Boolean () converts its parameters into a Boolean value and returns a Boolean object containing the value. When called as a function (without the new operator), Boolean () only converts its parameters into an original Boolean value and returns this value. Note: If the value parameter is omitted or set to 0,-0, null, "", false, undefined, Or NaN, the object is set to false. Otherwise, set it to true (even if the value parameter is a string "false ").


Boolean Object Attributes
Attribute description
Constructor returns a reference to the Boolean function that creates this object.
Prototype enables you to add attributes and methods to an object.
Boolean object Method
Method description
ToSource () returns the source code of the object.
ToString () converts the logical value to a string and returns the result.
ValueOf () returns the original value of the Boolean object.
Boolean Object Description

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.



(6). Math object

Math objects are used to execute mathematical tasks. Use the attributes and method Syntax of Math:

Var pi_value = Math. PI;
Var sqrt_value = Math. sqrt (15 );

Note: Math objects are not classes of objects as Date and String, So Math () is not a constructor, such as Math. A function like sin () is just a function, not a method of an object. You can call all attributes and methods of a Math object without creating it.

Math Object Attributes

Attribute description
E returns an arithmetic constant e, that is, the base number of the natural logarithm (approximately 2.718 ).
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 the number.
Acos (x) returns the arc cosine of the number.
Asin (x) returns the arc sine of the number.
Atan (x) returns the value of x between-PI/2 and PI/2 radian.

Returns the arc tangent value.
Atan2 (y, x) returns the angle (between-PI/2 and

PI/2 radian ).
The ceil (x) logarithm is rounded up.
Cos (x) returns the cosine of the number.
Exp (x) returns the e index.
The floor (x) logarithm is deprecated.
Log (x) returns the natural logarithm of the number (Base e ).
Max (x, y) returns the highest values in x and y.
Min (x, y) returns the lowest values in x and y.
Pow (x, y) returns the y Power of x.
Random () returns 0 ~ A random number between 1.
Round (x) rounds the number to the nearest integer.
Sin (x) returns the sine of the number.
Sqrt (x) returns the square root of the 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.


Example:

Document. write (Math. floor (2.3) + '<br/>'); // all math methods are static.
Document. write (Math. ceil (2.3) + '<br/> ');
Document. write (Math. round (2.3) + '<br/> ');
Document. write (Math. sqrt (10) + '<br/> ');
Document. write (Math. random () * 5 + 5 + '<br/>'); // return [5, 10)
Document. write (Math. random () + '<br/>'); // return)

/* Running result:

2
3
2
3.1622776601683795
8.213993671524253
0.3670194675527746

*/


(7). JavaScript Global Object

Global attributes and functions can be used for all built-in JavaScript objects. Top-level functions (global functions)
Function Description
DecodeURI () decodes an encoded URI.
DecodeURIComponent () decodes an encoded URI component.
EncodeURI () encodes a string into a URI.
EncodeURIComponent () encodes a string into a URI component.
Escape () is used to encode a string.
Eval () calculates the JavaScript string and runs it as script code.
GetClass () returns the JavaClass of a JavaObject.
IsFinite () checks whether a value is a finite number.
IsNaN () checks whether a value is a number.
Number () converts the object value to a Number.
ParseFloat () parses a string and returns a floating point number.
ParseInt () parses a string and returns an integer.
String () converts the object value to a String.
Unescape () decodes a string encoded by escape.
Top-level attributes (Global attributes)
Method description
Infinity represents a positive Infinity value.
Java represents a JavaPackage at the java. * package level.
NaN indicates whether a value is a numeric value.
Packages root JavaPackage object.

Undefined indicates an undefined value.


Global Object Description a global object is a predefined object and serves as a placeholder for global functions and global attributes of JavaScript. By using global objects, you can access all other predefined objects, functions, and attributes. The global object is not an attribute of any object, so it has no name. In the top-level JavaScript code, you can use the keyword "this" to reference a global object. However, you do not need to reference a global object in this way, because the global object is the header of the scope chain, which means that all non-restrictive variables and function names will be queried as attributes of the object. For example, when JavaScript code references the parseInt () function, it references the parseInt attribute of the global object. The global object is the header of the scope chain. It also means that all variables declared in the top-level JavaScript code will become attributes of the global object. A global object is only an object, not a class. Neither a constructor nor a new global object can be instantiated. When JavaScript code is embedded into a special environment, global objects usually have environment-specific attributes. In fact, the ECMAScript standard does not specify the type of global objects. JavaScript implementation or embedded JavaScript can take any type of objects as global objects, as long as this object defines the basic attributes and functions listed here. For example, in JavaScript implementation that allows scripting of Java through LiveConnect or related technologies, the global object is assigned the java and Package attributes listed here and the getClass () method. In the client JavaScript, The Global object is the Window object, indicating that the Web browser Window of JavaScript code is allowed.
Example
In the core JavaScript language, the predefined attributes of a global object cannot be enumerated. All the global variables that can be implicitly or explicitly declared can be listed in a for/in loop, as shown below:

Var variables = "";

For (var name in this)
{
Variables + = name + "<br/> ";
}

Document. write (variables );


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.