JavaScript common local objects summary _javascript tips

Source: Internet
Author: User
Tags local time mathematical constants natural logarithm numeric value rounds square root string format

One, JavaScript is the Object-oriented programming language

Encapsulation: the ability to store relevant information, whether data or method, in an object

Aggregation: The ability to store an object within another object

Inheritance: The ability of a class to be derived from another class (or classes) by its properties and methods.

Polymorphism: The ability to write functions or methods that can be run in multiple forms

Two, Array object

Use a separate variable name to store a series of values.

2.1 Creating an Array object

var avalues = new Array ();
var avalues = new Array;
var acolors = new Array ("Red", "green", "Blue");
Alert (acolors[1]);//Output green
acolors[0] = "white";//Modify the value of the first element
var acolors = new Array ();
Acolors[0] = "Red";
ACOLORS[1] = "green";
ACOLORS[2] = "Blue";

2.2 Creating an array using literal quantities

var acolors = ["Red", "green", "blue"];

2.3 Length of the array

var acolors = new Array ("Red", "green", "Blue");
alert (acolors.length);//Output 3 
var acolors = new Array ("Red", "green", "Blue");
alert (acolors.length);//Output 3
acolors[20]= "BLACK";
alert (acolors.length);//Output 21

2.4 Traversal of an array

var subjects=["Html/css/js", "Web form", "Ajax", "N-tiers", "Oracle";
for (Var i=0;i<subjects.length;i++) {
alert (subjects[i]);
} 
var subjects=["Html/css/js", "Web form", "Ajax", "N-tiers", "Oracle";
For (var index in subjects) {
alert (Subjects[index]);
}

2.5 Arrays of properties and methods

Length Sets or returns the number of elements in the array.

Join () to concatenate all the elements of an array into a string by the specified delimiter.

Reverse () Reverses the order of the elements in the array.

Sort () The elements of the array are sorted

Arrayobject.sort (SortBy)

The parameter is optional. Specify the collation. Must be a function.

The value returned is a reference to the array. Note that the array is sorted on the original array and no replicas are generated.

Custom Collation

If you want to sort by other criteria, you need to provide a comparison function that compares two values, and then returns a number that describes the relative order of the two values.

The comparison function should have two parameters A and B, and the return value would be as follows: If A is less than B, a value less than 0 is returned before a in the sorted array, and a should appear before B. If a equals B, it returns 0. If a is greater than B, a value greater than 0 is returned.

Concat (array array)

Connects two or more arrays and returns the result.

Array Slice (number Start,number end)

Returns the selected element from an existing array

var messages4 = messages3.slice (0, 5);

Splice () Deletes the element and adds a new element to the array.

Messages4.splice (0, 2, "dddd"); 0 indicates the location of the element at which the deletion begins, 2 is the length of the deletion, the added element is dddd, and the deletion location is added

Push () adds one or more elements to the end of the array and returns a new length.

Pop () deletes and returns the last element of the array

Unshift () adds one or more elements to the beginning of the array and returns a new length.

Shift () deletes and returns the first element of the array

Application of 2.6 arrays

var messages = new Array ("", "", "");
Messages[0] = "Beijing";
MESSAGES[1] = "Shanghai";
MESSAGES[2] = "Zhejiang";
MESSAGES[3] = "Hunan"; 
For (var index in messages) {Document.writeln ("traversal array"); document.write (Messages[index); document.write ("<br>");
var s = messages.join (', ');
Document.writeln ("join array");
document.write (s);
document.write ("<br>");
Messages.reverse ();
For (var index in messages) {Document.writeln ("after Reverse"); document.write (Messages[index]); document.write ("<br>");
Messages.sort ();
For (var index in messages) {Document.writeln (after sort); document.write (Messages[index]); document.write (" 

The above code output results are:

Traversal array Beijing

Traversal array Shanghai

Traversal array Zhejiang

Traversal array Hunan

Join Array Beijing,shanghai,zhejiang,hunan

Hunan after reverse

Zhejiang after reverse

Shanghai after reverse

Beijing after reverse

Sort after Beijing

Sort after Hunan

Sort after Shanghai

Sort after Zhejiang

After merging Beijing

After merging Hunan

After merging Shanghai

After merging Zhejiang

After merging a

After merge C

After merging B

Select Slice (0,5) after Beijing

Select Slice (0,5) after Hunan

Select Slice (0,5) after Shanghai

Select Slice (0,5) after Zhejiang

Select Slice (0,5) after a

Third, date objects

The Date object is used to obtain and process dates and times.

3.1 Definition Date

var mydate=new Date ();
var mydate=new Date (2009, 7, 20); 2009-08-20 

3.2 Method of Date Object

3.2.1toXXX Method Group

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 () Converts the Date object to a string based on GMT.

toUTCString () Converts a Date object to a string based on the universal.

toLocaleString ()

Converts a Date object to a local time format string.

toLocaleTimeString ()

Converts the time portion of a Date object to a local time format string

toLocaleDateString ()

Converts the date part of a Date object to a local time format string

3.2.2getXXX Method Group

GetDate () returns one day of one months from the Date object (1 ~ 31)

Getday () returns one day of the week from the Date object (0 ~ 6)

GetMonth () returns the month from the date object (0 ~ 11)

getFullYear () returns the year with a four-digit number from the Date object

GetYear () returns the year with a two-bit or four-digit number from the Date object.

GetHours () returns the hour of the date object (0 ~ 23)

Getminutes () returns the Minutes of the date object (0 ~ 59)

Getseconds () returns the number of seconds (0 ~ 59) of the Date object.

Getmilliseconds ()

Returns the milliseconds for the date object (0 ~ 999)

GetTime () returns the number of milliseconds since January 1, 1970

3.2.3setXXX Method Group

Setdate () Sets the day of the month in the Date object (1 ~ 31))

Setmonth () Sets the month in the Date object (0 ~ 11))

setFullYear () sets the year in the Date object (four digits)

Setyear () sets the year in the Date object (two-bit or four-digit number).

Sethours () Sets the hours in the Date object (0 ~ 23)

Setminutes () Sets the minutes in the Date object (0 ~ 59)

Setseconds () Sets the seconds in the Date object (0 ~ 59)

Setmilliseconds ()

Set the milliseconds in the Date object (0 ~ 999)

SetTime () calculates the date and time by adding or subtracting the specified number of milliseconds to the time origin

Four, String objects

A String object represents a sequence of Unicode characters.

The length property of a String object declares the number of characters in the string.

The string class defines a large number of methods for manipulating strings.

4.1 Properties

Length of string

4.2 Methods related to string format control

Big (), small () displays a string with a large font and a small font size body.

Bold () displays the string in bold.

Italics () displays the string in italics.

Strike () uses strikethrough to display strings.

FontColor () Displays the string using the specified color.

FontSize () Displays the string using the specified dimensions.

The sub (), SUP () displays the string as subscript, superscript.

toLowerCase (), toUpperCase ()

Converts the string to lowercase and uppercase.

Fixed () displays the string in typewriter text.

Link () Displays the string as a link.

Anchor () creates an HTML anchor.

4.3 methods related to string content manipulation

CharAt () returns the character at the specified position.

charCodeAt () returns the Unicode encoding of the character at the specified location.

Concat () connection string.

fromCharCode ()

Creates a string from a character encoding.

IndexOf (), LastIndexOf ()

Retrieves a string from the previous back. Searches for a string from the back forward.

Localecompare ()

Compares two strings in a local-specific order.

Match () finds one or more matches that are being expressed.

Replace (), search ()

Replaces, retrieves a substring that matches a regular expression.

Slice () extracts a fragment of a string.

Split () splits the string into an array of strings.

SUBSTR () extracts the specified number of characters from the starting index number in the string.

SUBSTRING () extracts the characters between the two specified index numbers in the string.

4.4 Application

var strings = new String ("AbcDEfG");
document.write ("String Value in Strings:");
document.write (strings);
document.write ('  

V. Math Object

The Math object is a mathematical tool that contains a number of static read-only properties that represent mathematical constants and static methods for performing mathematical operations.

E constant E, base of natural logarithm (approximately equal to 2.718)

LN2 returns 2 of the natural logarithm (about 0.693)

LN10 returns 10 of the natural logarithm (about 2.302)

LOG2E returns the logarithm of the 2-based E (about 1.414)

LOG10E returns the logarithm of the 10-based E (about 0.434)

Pi returns PI (approximately equal to 3.14159)

Sqrt1_2 returns 2 squared to eradicate 1 (about 0.707)

SQRT2 returns the square root of 2 (approximately equal to 1.414)

Absolute value of ABS (x) return number

cos (x), ACOs (x)

Returns the sine, inverse cosine of a number

Sin (x), ASIN (x)

Returns the sine, inverse chord value of a number

Tan (x), Atan (x)

Returns the tangent of the angle, the inverse tangent of the number of radians between-PI/2 and PI/2

ATAN2 (y,x) returns the angle from the X axis to point (X,y)

Ceil (x), floor (x)

Rounds a number to the top. Rounds a number to the next.

EXP (x), log (x) returns the exponent of E. Returns the natural logarithm of a number (bottom e)

Max (x,y), Min (x,y)

Returns the highest value in X and Y

POW (x,y) returns the Y-power of X

Random () returns the random number between 0 ~ 1

Round (x) rounding a number to the nearest integer

SQRT (x) returns the square root of a number

Vi. Global Objects

Global objects are predefined objects that are placeholders for global functions and global properties of JavaScript.

By using global objects, you can access all other predefined objects, functions, and properties.
The global object is not a property of any object, so it has no name. It has neither a constructor nor an instantiation of a new global object, and all its members are static.

6.1 Global Properties

Infinity represents the value of a positive infinity.

NaN Indicates whether a value is a numeric value.

Undefined indicates an undefined value.

6.2 Global Functions

decodeURI () decodes an encoded URI.

decodeURIComponent ()

Decodes an encoded URI component.

encodeURI () encodes the string as a URI.

encodeURIComponent ()

Encodes the string as a URI component.
Eval () evaluates the string and executes it as a script code.

Isfinite () checks to see if a value is a number that has a poor size.

isNaN () checks whether a value is a number.

The isNaN () function is commonly used to detect the results of parsefloat () and parseint () to determine whether they represent legitimate numbers. Of course, you can also use the isNaN () function to detect arithmetic errors, such as the case of dividing by 0.

Parsefloat () parses a string and returns a floating-point number.

parseint () parses a string and returns an integer.

Vii. Summary

JavaScript is an object-oriented language with the ability to customize objects. At the same time, it can be based on existing objects programming, contains ECMAScript local objects, as a host of the browser running environment provides a set of browser objects and Web pages provided objects.

A local object is a set of reference types that are provided by ECMAScript, and it is ECMAScript that some objects are implemented, regardless of the specific script host.
An array object is actually a set of length dynamics, which contains the length attributes and sorting, inversion, and concatenation methods.

A Date object represents a particular moment. The browser's native system time can be obtained by its constructor. Or specify a specific datetime, which contains a set of get methods that can be used to obtain information about a part of a Date object, or a set of get methods to change some methods of a Date object, or to obtain a string form of a Date object through its set of to methods.

A String object represents a sequence of 0 or more Unicode characters. It contains the length property and a set of methods for formatting transformations and for manipulating the characters in them.

Global objects contain a series of global properties and global functions that are not directly used by any object referenced by them.

A mathematical object contains a set of static read-only properties to represent mathematical constants, and a set of static methods to implement commonly used mathematical operations.

About JavaScript common local objects small series to introduce so many people, I hope to help you!

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.