JavaScript Common Objects

Source: Internet
Author: User
Tags getdate local time natural logarithm sin square root



Several common objects and their properties and usage:

(1). Array Object

The Array object is used to store multiple values in a single variable.
To create the syntax for an Array object:

New Array ();
New Array (size);
New Array (Element0, Element1, ..., ELEMENTN);

Parameters

The parameter size is the expected number of array elements. Returns an array of length fields that will be set to the value of size. Parameter element ..., ELEMENTN is a list of parameters. When you use these parameters 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 you do not use parameters when calling the constructor array (), the returned array is empty and the length field is 0. When the constructor is called, only one numeric argument is passed to it, and the constructor returns an array with the specified number and element undefined. When an array () is called by another parameter, the constructor initializes the array with the value specified by the parameter. When a constructor is called as a function, and the new operator is not used, its behavior is exactly the same as when it is called with the new operator.


Array Object Properties
Property Description
Constructor returns a reference to the array function that created this object.
Length Sets or returns the number of elements in the array.
Prototype gives you the ability to add properties and methods to objects.

Array Object method
Method description
Concat () joins two or more arrays and returns the result.
Join () Places all elements of an array into a string. Element is entered by the specified delimiter.

Row Delimited.
Pop () deletes and returns 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 () deletes and returns the first element of the array
Slice () returns the selected element from an existing array
Sort () Sorts the elements of an array
Splice () Deletes the element and adds 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.
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 the 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] = "a";
document.write (arr+ ' <br/> ');//hello,world,hahaha
document.write (Arr.concat (' 123 ') + ' <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 ());//


/* Run Result:

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

*/


(2). Date Object

The Date object is used to process dates and times.
To create the syntax for a Date object:

var mydate=new Date ()

Note: The Date object automatically saves the current date and time as its initial value.
Date Object Properties
Property Description
Constructor returns a reference to the Date function that created this object.
Prototype gives you the ability to add properties and methods to objects.
Date Object method
Method description
Date () Returns the day and time.
GetDate () Returns the day of the one month (1 ~ 31) from the Date object.
GetDay () Returns the day of the week (0 ~ 6) from the Date object.
GetMonth () returns the month (0 ~ 11) from the Date object.
getFullYear () returns the year as a four-digit number from a Date object.
GetYear () Please use the getFullYear () method instead.
GetHours () returns the hour (0 ~ 23) of the Date object.
Getminutes () returns the minute (0 ~ 59) of the Date object.
Getseconds () returns the number of seconds (0 ~ 59) of the Date object.
Getmilliseconds () returns the milliseconds (0 ~ 999) of the Date object.
GetTime () returns the number of milliseconds since January 1, 1970.
getTimezoneOffset () returns the local time with Greenwich Mean Time (GMT)

Minute difference.
getUTCDate () Returns the day of the month (1 ~ 31) from the Date object according to the world.
GetUTCDay () Returns the day of the week (0 ~ 6) from the Date object according to the world.
getUTCMonth () returns the month (0 ~ 11) from the Date object, depending on the world.
getUTCFullYear () returns a four-digit year from a Date object based on world time


getUTCHours () returns the hour (0 ~ 23) of the Date object according to the universal.
getUTCMinutes () minute (0 ~ 59) of returning a Date object according to the Universal


getUTCSeconds () returns the second of a Date object according to the universal (0 ~ 59)


getUTCMilliseconds () milliseconds to return a Date object based on the universal (0 ~ 999)


Parse () returns milliseconds from Midnight January 1, 1970 to a specified date (string)

Number.
SetDate () Sets the day of the month in the Date object (1 ~ 31).
Setmonth () sets the month (0 ~ 11) in the Date object.
setFullYear () sets the year (four digits) in the Date object.
Setyear () Please use the setFullYear () method instead.
Sethours () sets the hour (0 ~ 23) in the Date object.
Setminutes () sets the minute (0 ~ 59) in the Date object.
Setseconds () sets the second (0 ~ 59) in the Date object.
Setmilliseconds () sets the milliseconds (0 ~ 999) in the Date object.
SetTime () Sets the Date object in milliseconds.
SetUTCDate () Sets the day of the month in the Date object according to the world time (1 ~ 31).
setUTCMonth () sets the month (0 ~ 11) in the Date object according to the world time.
setUTCFullYear () sets the year in the Date object according to the world time (four-bit

Number).
setUTCHours () sets the hour (0 ~ 23) in the Date object according to the world time.
setUTCMinutes () Sets the minutes in the Date object according to the world time (0 ~

59).
setUTCSeconds () Sets the seconds in the Date object according to the world time (0 ~

59).
setUTCMilliseconds () Sets the milliseconds in the Date object according to the world time (0 ~

999).
Tosource () returns the source code of the object.
ToString () Converts the Date object to a string.
toTimeString () Converts the time portion of a Date object to a string.
toDateString () Converts the date part of a Date object to a string.
toGMTString () Please use the toutcstring () method instead.
toUTCString () Converts the Date object to a string based on the universal.
toLocaleString () Converts a Date object to a word based on the local time format

Character string.
toLocaleTimeString () takes the time part of a date object according to the local time format

into a string.
toLocaleDateString () based on the local time format, the date part of a Date object

into a string.
UTC () returns the number of milliseconds from January 1, 1970 to the specified date, depending on the 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 18:30:55 gmt+0800 (China Standard Time)


/* Run Result:

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

*/


(3). String Object

The string object is used to manipulate text (strings).
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 operator new are used as constructors, it returns a newly created string object that holds the string representation of the string s or S. When a string () is not called with the new operator, it only converts s to the original string and returns the converted value.


String Object Properties
Property Description
Constructor a reference to the function that created the object
Length of long string
Prototype allows you to add properties and methods to an object
String Object method
Method description
Anchor () creates an HTML anchor.
Big () displays the string in a large font.
Blink () displays the flashing string.
Bold () uses bold to display the string.
CharAt () returns the character at the specified position.
charCodeAt () returns the Unicode encoding of the character at the specified position.
The concat () connection string.
Fixed () displays the string as a typewriter text.
FontColor () Displays the string using the specified color.
FontSize () Displays the string using the specified dimensions.
fromCharCode () creates a string from character encoding.
IndexOf () retrieves a string.
Italics () uses italics to display strings.
LastIndexOf () searches the string from the back forward.
Link () Displays the string as a link.
Localecompare () compares two strings in a local-specific order.
Match () finds the matching of one or more regular expressions.
Replace () replaces the substring that matches the regular expression.
Search () Retrieves the value that matches the regular expression.
Slice () extracts a fragment of a string and returns the extracted text in a new string.

Part.
Small () uses a small font size to display the string.
Split () splits the string into an array of strings.
Strike () use strikethrough to display strings.
Sub () displays the string as subscript.
SUBSTR () extracts the specified number of characters from the starting index number in the string.
SUBSTRING () extracts the characters between two specified index numbers in a string.
The SUP () displays the string as superscript.
toLocaleLowerCase () Converts the string to lowercase.
toLocaleUpperCase () Converts the string to uppercase.
toLowerCase () Converts the string to lowercase.
toUpperCase () Converts the string to uppercase.
Tosource () represents 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 type of data for JavaScript. The length property of the string object declares the number of characters in the string. The string class defines a number of methods for manipulating strings, such as extracting characters or substrings from a string, or retrieving characters or substrings. It is important to note that the JavaScript string is immutable (immutable), and the method defined by the string class cannot alter the contents of the string. A method like String.touppercase () returns a completely new string instead of modifying the original string. In the earlier JavaScript implementations of the Netscape code base (for example, in Firefox implementations), the string behaves like a read-only character array. For example, extracting a third character from the string s, you can use s[2] instead of a more standard S.charat (2). Also, when you apply a for/in loop to a string, it enumerates an array subscript for each character in the string (but be aware that the length property cannot be enumerated by the ECMAScript standard). Because the array behavior of strings is not standard, you should avoid using it.


Example:
var str = ' HelloWorld ';
/*alert ((Str.indexof (' he ') >= 0)? ' Find ': ' not find '); */
document.write (Str.indexof (' mm ') + ' <br/> ');
document.write (Str.fontcolor ("blue") + ' <br/> ');
document.write (Str.strike () + ' <br/> ');//Display a string with a delete symbol
document.write (Str.concat ("HI") + ' <br/> ');
document.write (str.touppercase () + ' <br/> ');
document.write (str.tolowercase () + ' <br/> ');
document.write (str.replace (' he ', ' Nihao ') + ' <br/> ');

/* Run Result:

-1
HelloWorld
HelloWorld
Helloworldhi
HELLOWORLD
HelloWorld
nihaolloworld*/


(4). Number Object

The number object is the wrapper object for the original value.
To create the syntax for the number object:

var mynum=new number (value);
var mynum=number (value);

Parameters

The parameter value is the numeric value of the number object to be created, or the values to be converted to numbers.

return value

When number () and operator new are used as constructors, it returns a newly created number object. If you do not use the new operator to call number () as a function, it will convert its arguments to an original value, and return this value (NaN if the conversion fails).


Number object property

Property Description
Constructor returns a reference to the number function that created this object.
The maximum number that the max_value can represent.
The minimum number that the min_value can represent.
NaN non-numeric value.
Negative_infinity negative infinity, this value is returned when overflow.
Positive_infinity is infinity and returns this value when it overflows.
Prototype gives you the ability to add properties and methods to objects.
Number Object method
Method description
ToString converts the number to a string, using the specified cardinality.
toLocaleString converts a number to a string, using the local number format order.
ToFixed converts a number to a string, and the result has a specified number of digits after the decimal point.

Digital.
Toexponential converts the value of an object to an exponential count method.
Toprecision format the number to the specified length.
ValueOf returns the base numeric value of a number object.
Number Object Description

In JavaScript, numbers are a basic type of data. JavaScript also supports the number object, which is the wrapper object for the original value. JavaScript automatically transitions between the original data and the object, if necessary. In JavaScript 1.1, you can explicitly create a number object with the constructor number (), although it is not necessary to do so. The constructor number () may not be used with operator new and is used directly as a conversion function. When number () is called in this way, it converts its argument to a digit, and then returns the converted original value (or NaN). Constructors are also commonly used as placeholders for 5 useful numeric constants, the 5 useful numeric constants are the maximum number that can be represented, the smallest number that can be represented, the positive infinity, the negative infinity, and the special NaN value. Note that these values are the properties of the constructor number () itself, rather than the properties of a single Value object.


For example, using the attribute max_value is correct:

var big = Number.MAX_VALUE

But this is wrong:

var n= new number (2);
var big = N.max_value

As a comparison, let's look at the other methods of the toString () and number objects, which are the methods of each number object, not the number () constructor. As mentioned earlier, when necessary, JavaScript automatically converts the original value into a number object, and the numbers method can be called either as a numeric object or as a raw numerical value.

var n = 123;
var binary_value = n.tostring (2);


(5). Boolean Object

A Boolean object represents two values: "True" or "false".
Syntax for creating a Boolean object:

New Boolean (value); constructor function
Boolean (value); Conversion functions

Parameters

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

return value


When called as a constructor (with operator new), Boolean () converts its arguments to a Boolean value and returns a Boolean object that contains the value. If called as a function (without operator new), Boolean () will only convert its arguments to an original Boolean value and return this value. Note: If the value parameter is omitted, or is set to 0,-0, NULL, "", false, undefined, or NaN, the object is set to False. Otherwise set to true (even if the value parameter is the string "false").


Boolean Object Properties
Property Description
Constructor returns a reference to the Boolean function that created this object
Prototype gives you the ability to add properties and methods to objects.
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 packages a Boolean value. The Boolean object is primarily used to provide a toString () method that converts a Boolean value into a string. When the ToString () method is called to convert a Boolean value to a string (typically called implicitly by JavaScript), JavaScript inherently converts the Boolean value to a temporary Boolean object and then calls the object ToString () method.



(6). Math Object

The Math object is used to perform mathematical tasks. Syntax for using the properties and methods of Math:

var Pi_value=math.pi;
var sqrt_value=math.sqrt (15);

Note: The Math object is not a class of objects like Date and String, so there is no constructor for math (), a function like Math.sin () is just a function, not a method of an object. You do not have to create it, and you can invoke all of its properties and methods by using Math as an object.

Math Object Properties

Property Description
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 (approximately equal to 1.414) with a base of 2.
LOG10E returns the logarithm of E (approximately equal to 0.434) with a base of 10.
Pi returns PI (approximately equal to 3.14159).
Sqrt1_2 returns the reciprocal of the square root that returns 2 (approximately equals 0.707).
SQRT2 returns the square root of 2 (approximately equal to 1.414).
Math Object method
Method description
ABS (x) The absolute value of the return number.
ACOs (x) returns the inverse cosine of the number.
ASIN (x) returns the inverse chord value of the number.
Atan (x) returns an X with a value between-PI/2 and PI/2 radians.

Anyway tangent value.
ATAN2 (y,x) returns the angle from the x-axis to point (x, y) (between-PI/2 and

Between PI/2 radians).
Ceil (x) is rounded on the logarithm.
COS (x) returns the cosine of the number.
EXP (x) returns the exponent of E.
Floor (x) is rounded down with a logarithmic.
Log (x) returns the natural logarithm of the number (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.
The POW (x, y) returns the y power of X.
Random () returns an arbitrary number between 0 and 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/> ');//math are all static methods.
document.write (Math.ceil (2.3) + ' <br/> ');
document.write (Math.Round (2.3) + ' <br/> ');
document.write (Math.sqrt (Ten) + ' <br/> ');
document.write (Math.random () *5+5+ ' <br/> ');//return [5,10]
document.write (Math.random () + ' <br/> '); Back to [0,1]

/* Run Result:

2
3
2
3.1622776601683795
8.213993671524253
0.3670194675527746

*/


(7). JavaScript Global Objects

Global properties and functions are available for all built-in JavaScript objects. Top-level functions (global functions)
Function description
decodeURI () decodes a coded URI.
decodeURIComponent () decodes a coded URI component.
encodeURI () encodes the string as a URI.
encodeURIComponent () encodes the string as a URI component.
Escape () encodes the string.
Eval () computes the JavaScript string and executes it as script code.
GetClass () returns a Javaobject javaclass.
Isfinite () checks if a value is a number that has a poor size.
IsNaN () checks whether a value is a number.
Number () Converts the value of the object to a digit.
Parsefloat () parses a string and returns a floating-point number.
parseint () parses a string and returns an integer.
String () Converts the value of the object to a string.
Unescape () decodes a string encoded by escape ().
Top-level properties (global properties)
Method description
Infinity represents the numeric value of the positive infinity.
Java represents a javapackage at the Java.* package level.
NaN Indicates whether a value is a numeric value.
Packages the root Javapackage object.

Undefined indicates a value that is not defined.


Global objects describe global objects as pre-defined objects, as placeholders for JavaScript global functions and global properties. By using global objects, you can access all of the other predefined objects, functions, and properties. The global object is not a property of any object, so it does not have a name. In the top-level JavaScript code, you can refer to the global object with the keyword this. However, it is not usually necessary to refer to the global object in this way because the global object is the header of the scope chain, which means that all unqualified variables and function names are queried as properties of the object. For example, when the JavaScript code references the parseint () function, it refers to the parseint property of the global object. A global object is the head of a scope chain and also means that all variables declared in the top-level JavaScript code will be properties of the global object. A global object is just an object, not a class. There is neither a constructor function nor a new global object to instantiate. When JavaScript code is embedded in a special environment, global objects typically have environment-specific properties. In fact, the ECMAScript standard does not specify the type of global objects, JavaScript implementations or embedded JavaScript can use any type of object as a global object, as long as the object defines the basic properties and functions listed here. For example, in JavaScript implementations that allow scripting Java through LiveConnect or related techniques, global objects are given the Java and package attributes listed here, and the GetClass () method. In client-side JavaScript, the global object is the Window object, which represents the Web browser windows that allow JavaScript code.
Example
In the JavaScript core language, the predefined properties of global objects are non-enumerable, and all global variables that are implicitly or explicitly declared can be listed in For/in Loop, as follows:

var variables = "";

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

document.write (variables);


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript Common Objects

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.