JavaScript often uses objects

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



Several common objects and their properties and how to use them:

(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);

Number of references

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 participants. When these parameters are used to invoke 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.

Assuming that no parameters are used when calling the constructor array (), the returned array is empty and the length field is 0.

When the constructor is called, only a number of arguments are 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 number of parameters.

When the constructor is called as a function. When you do not use the new operator, it behaves exactly the same as when you invoke it using the new operator.


Array Object Properties
Descriptive description of attributes
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 descriptive narration
Concat () joins two or more other arrays and returns the result.
Join () places all the 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 () joins one or more other 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 other elements to the beginning of the array. and returns the new length


ValueOf () returns the original value of the array object


Examples:

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 ());//


/* Execution Results:

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 ()

Gaze: The Date object will voluntarily save the current date and time as its initial value.
Date Object Properties
Descriptive description of attributes
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 descriptive narration
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 from the Date object (1 ~ 31), based on the world.
GetUTCDay () Returns the day of the week (0 ~ 6) from the Date object, based on world time.
getUTCMonth () returns the month (0 ~ 11) from the Date object, based on the world.
getUTCFullYear () returns a four-digit year from a Date object based on world time


getUTCHours () the hour (0 ~ 23) that returns the Date object based on the world.
getUTCMinutes () the minute (0 ~ 59) that returns a Date object based on world time


getUTCSeconds () Seconds to return a Date object based on world time (0 ~ 59)


getUTCMilliseconds () milliseconds to return a Date object based on world time (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 (1 ~ 31) based on the world time.
setUTCMonth () sets the month (0 ~ 11) in the Date object based on the world time.
setUTCFullYear () sets the year in the Date object based on the world time (four-bit

Number).


setUTCHours () sets the hour (0 ~ 23) in the Date object based on the world time.


setUTCMinutes () Sets the minutes in the Date object based on the world time (0 ~

59).
setUTCSeconds () Sets the seconds in the Date object based on the world time (0 ~

59).


setUTCMilliseconds () Sets the milliseconds in the Date object based on 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 () According to the world time. Converts a Date object to a string.


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 () The date portion of a Date object according to the local time format

into a string.
UTC () returns the number of milliseconds from January 1, 1970 to the specified date, based on world time.
ValueOf () returns the original value of the Date object.



Examples:

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)


/* Execution Results:

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);

Number of references

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 together as constructors. It returns a newly created string object that holds the string representation of the string s or S.

When a String () is called without the new operator. It simply converts s to the original string and returns the converted value.


String Object Properties
Descriptive description of attributes
Constructor a reference to the function that created the object
Length of long string
Prototype agrees that you are adding properties and methods to objects
String Object method
Method descriptive narration
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 () uses a local-specific order to compare two strings.
Match () finds one or more regular table-matching matches.
Replace () replaces substrings that match the regular table.
Search () Retrieves values that match the normal table.
Slice () extracts a fragment of a string. and returns the extracted in the new string.

Part.


Small () uses a small font size to display the string.
Split () cuts 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 string from the starting index.
SUBSTRING () extracts the characters between two specified indexes 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.


Description of the String object

A string is a primary 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 retrieve characters or substrings.

It is important to note that. The string of JavaScript is immutable (immutable), and the method defined by the string class cannot alter the contents of the string.

Like String.touppercase (), this scheme returns a completely new string instead of altering the original string. In the early JavaScript implementations of the Netscape code base (such as in Firefox implementations), the string behaves like a read-only character array.

For example, extracting the third character from the string s can be used to replace a more standard S.charat (2) with s[2]. Also, when you apply a for/in loop to a string. It enumerates the array subscripts for each character in the string (but be aware that the ECMAScript standard specifies. The length property cannot be enumerated). Because the array behavior of the string is not standard. So you should avoid using it.


Examples:
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/> ');

/* Execution Results:

-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);

Number of references

The parameter value is the numeric value of the number object to be created. or a value to convert to a number.

return value

When number () and operator new are used as constructors. It returns a newly created number object.

Suppose you don't use the new operator to call number () as a function. It converts its own number of references to a raw value, and returns this value (if the conversion fails, it returns NaN).


Number object property

Descriptive description of attributes
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 descriptive narration
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. The specified number of digits after the decimal point of the result.

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 Descriptive narrative

In JavaScript, numbers are a major type of data.

JavaScript also supports the number object, which is the wrapper object for the original value. When necessary. JavaScript will actively convert between the original data and the object itself. In JavaScript 1.1, you can clearly create a number object with the constructor number (), although it is not necessary to do so. The constructor number () can be used without the operator new and directly as a conversion function. When number () is called in such a way. It converts its own number of references into a number. The converted original value (or NaN) is then returned. Constructors are also commonly used as placeholders for 5 useful numeric constants, each of which 5 practical 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. Attention. 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 will voluntarily convert the original value into a number object. The number method is called to both the numeric object and the original 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

Number of references

The value of the parameter value that 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 parameters to a Boolean value. And returns a Boolean object that includes the value. If called as a function (without operator new), the Boolean () will simply convert its parameters to a primitive Boolean value and return this value. Gaze: The object is set to False if the value parameter is omitted or set to 0,-0, NULL, "", false, undefined, or NaN. Otherwise set to true (even if the value parameter is the string "false").


Boolean Object Properties
Descriptive description of attributes
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 descriptive narration
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 Descriptive narrative

In JavaScript, a Boolean value is a primary 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 this boolean value to a temporary Boolean object, and then calls the object ToString () method.



(6). Math Object

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

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

Gaze: The Math object is not a class of objects like Date and String. So there is no constructor for Math (). Like Math.sin () This function is just a function. is 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

Descriptive description of attributes
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 descriptive narration
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.


Examples:

document.write (Math.floor (2.3) + ' <br/> ');//math 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]

/* Execution Results:

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)
Descriptive description of function
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 runs 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 descriptive narration
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 Object Description Global objects are pre-defined objects that act as placeholders for JavaScript global functions and global properties. By using global objects, you can access all other pre-defined objects, functions, and properties. The global object is not a property of whatever object, so it has no name. In the top-level JavaScript code, you can refer to the Global object with keyword this.

However, it is not usually necessary to refer to the global object in such a way that 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 refers to the parseint () function, it refers to the parseint property of the global object. The global object is the head of the 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 simply 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. As a matter of fact. The ECMAScript standard does not specify the type of the global object. JavaScript implementations or embedded JavaScript can use arbitrary types of objects as global objects. Just want the object to define the basic properties and functions listed here. Like what. In JavaScript implementations that agree to script Java through LiveConnect or related technologies, global objects are given the Java and package properties listed here, as well as the GetClass () method. and in client JavaScript. A global object is a Window object that represents a Web browser form that consents to JavaScript code.
Sample Example
In the JavaScript core language, the pre-defined properties of global objects are non-enumerable, all of which can be used for/in to loop through all implicitly or explicitly declared global variables. For example, see the following:

var variables = "";

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

document.write (variables);


JavaScript often uses 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.