Javascript-Basic Object

Source: Internet
Author: User
Tags addall array definition time zones natural logarithm

The basic object is an internal object. In JavaScript, there are two situations for referencing object attributes and methods:

Static internal object, that is, you do not need to create an instance for the object when referencing its attributes or methods;

Dynamic internal objects and instance objects must be created for the objects or methods that reference them.

1. When creating a custom object and adding attributes dynamically, the object is generally used.

Each object contains the tostring method. The tostring method has different parameters.

2. Number "Number" object.

This object is rarely used. It is more of a "Number" object, that is, a "variable.
Attribute:
Max_value usage: Number. max_value; "maximum value" is returned ".
Min_value usage: Number. min_value; returns the "minimum value ".
Nan usage: Number. Nan or Nan; "Nan" is returned ". "Nan" (not a numerical value) was introduced very early.
Negative_infinity usage: Number. negative_infinity; Return Value: negative infinity, smaller than the "minimum" value.

Positive_infinity usage: Number. positive_infinity; Return: positive infinity, greater than the "maximum" value.

Method:

Tostring () usage: <value variable>. tostring (); returns a value in the string format. For example, if a = 123, A. tostring () = '000000 '.

3. String object. The simplest, quickest, effective, and commonly used method for declaring a String object is to assign values directly.

Attribute:

There is only one attribute length. Usage: <String object>. length; returns the length of the string.

Method:
Charat () usage: <String object>. charat (<position>); returns a single character with the <position> character of the string.
Charcodeat () usage: <String object>. charcodeat (<position>); returns the ASCII code of a single character whose character is at the <position> position.
Fromcharcode () usage: string. fromcharcode (a, B, c ...); returns a string. the ASCII code of each character is composed of a, B, c... and so on.
Indexof () usage: <String object>. indexof (<another string object> [, <start position>]); this method finds the <another string object> from the <String object> (if the <start position> is given, the position before it is ignored). If it is found, its position is returned, if no value is found, "-1" is returned ". All "positions" start from scratch.
Lastindexof () usage: <String object>. lastindexof (<another string object> [, <start position>]); similar to indexof (), It is found from the back.
Split () usage: <String object>. split (<delimiter>); returns an array separated from the <String object>, which is not included in the returned array.
Substring () usage: <String object>. substring (<Start> [, <End>]); returns the substring of the original string, this string is a segment of the original string from the <Start> position to the <End> position. <End>-<Start> = return the length of the string ). If <final> is not specified or the length exceeds the string length, the substring is retrieved from the <Start> position until the end of the original string. If the specified position cannot return a string, an empty string is returned.
Substr () usage: <String object>. substr (<Start> [, <long>]); returns the substring of the original string, which is a segment of the original string starting from the <Start> position with a length of <long>. If the length is not specified or exceeds the length of the string, the substring is obtained from the <Start> position until the end of the original string. If the specified position cannot return a string, an empty string is returned.
Tolowercase () usage: <String object>. tolowercase (); returns a string that converts all uppercase letters of the original string into lowercase letters.

Touppercase () usage: <String object>. touppercase (); returns a string that converts all lowercase letters of the original string into uppercase letters.

When the expression + contains a string, all the + parameters are treated as strings.

The big () method, bold () method, fontcolor ("red") method, anchor () method, and link () method are used to generate HTML tags.

You can directly treat "A" as a string object.

Charat () method, charcodeat () returns unicode encoding, such as Str. charcodeat (0). tostring (16); convert to hexadecimal format for viewing.

Indexof (), lastindexof (), match (), search (), replace (), split (), slice () method returns some characters, substr (), substring (), tolowwercase, touppercase

4. Array

Array object. An array object is a collection of objects. objects in an array can be of different types. Each member object in the array has a subscript to indicate its position in the array.
Array definition method:
VaR <array name> = new array ();
In this way, an empty array is defined. To add array elements later, use:
<Array name> [<subscript>] = ...;
The subscript representation of the array is enclosed in square brackets.
If you want to initialize data directly when defining an array, use:
VaR <array name> = new array (<element 1>, <element 2>, <element 3> ...);
For example, VAR myarray = new array (1, 4.5, 'Hi'); defines an array named myarray with the following elements: myarray [0] = 1; myarray [1] == 4.5; myarray [2] = 'hi '.
However, if there is only one element in the element list and this element is a positive integer, this defines an array containing <positive integer> null elements.
Note: javascript only has a one-dimensional array. To use multi-dimensional arrays, use this virtual method:
VaR myarray = new array (),...);

Another form of creating and initializing an array: var arr = [1, "a"];, when there is no content between two, it indicates an empty data undefined.
Attribute
Length attribute usage: <array Object>. length; returns the length of the array, that is, the number of elements in the array. To add an element, you only need to: myarray [myarray. Length] = ....
Method
Join () usage: <array Object>. Join (<separator>); returns a string that concatenates all elements in the array and places them between elements with a <separator>. This method does not affect the original content of the array.
Reverse () usage: <array Object>. Reverse (); reverse the element order in the array. If you use this method for the array [1, 2, 3], it will make the array: [3, 2, 1].
Slice () usage: <array Object>. slice (<Start> [, <End>]); returns an array, which is a subset of the original array, starting with <Start> and finally <End>. If <final> is not given, the subset is always taken to the end of the original array.
Sort () usage: <array Object>. Sort ([<method function>]); arrange the elements in the array in a certain order. If <method function> is not specified, it is alphabetically arranged. In this case, 80 is better than 9. If <method function> is specified, sort by the <method function> specified sorting method.
Sort numbers in ascending order:
Function sortmethod (a, B ){
Return A-B;
}

Myarray. Sort (sortmethod );
Sort the numbers in descending order: Convert "A-B" to "B-".

You can use the IF (obj. Length) statement to determine whether an object has the Length attribute and whether it is an array object from the page.

Array Implementation using objects: You can also declare a function object, add the Length attribute, and then create a new function object to create an array. In fact, an array is created in a similar format, rather than a real array object.

5. Math is a mathematical object that provides mathematical calculations for data. "Math. <Name>" is used. Static: static object
Attribute
E returns the constant E (2. 718281828 ...).
Ln2 returns the natural logarithm of 2 (LN 2 ).
Ln10 returns the natural logarithm of 10 (ln 10 ).
Log2e returns the logarithm of E with a low value of 2 (log2e ).
Log10e returns the logarithm (log10e) of E with a low value of 10 ).
Pi returns π (3. 1415926535 ...).
Sqrt1_2 returns the square root of 1/2.
Sqrt2 returns the square root of 2.
Method
ABS (x) returns the absolute value of X.
ACOs (x) returns the arc cosine of X (the cosine is equal to the angle of X), expressed in radians.
Asin (x) returns the arc sine of X.
Atan (x) returns the arc tangent of X.
Atan2 (x, y) returns the width and angle of the complex number corresponding to the interior point (x, y) in the complex plane, expressed in radians. The value ranges from-π to π.
Ceil (x) returns the smallest integer greater than or equal to X.
Cos (x) returns the cosine of X.
Exp (x) returns the X power of E (Ex ).
Floor (x) returns the largest integer less than or equal to X.
Log (x) returns the natural logarithm of X (ln x ).
Max (a, B) returns a large number in A and B.
Min (a, B) returns a small number in A and B.
Pow (n, m) returns the M power of N (nm ).
Random () returns a random number greater than 0 and less than 1.
Round (x) returns the value rounded to X.
Sin (x) returns the sine of X.
SQRT (x) returns the square root of X.
Tan (x) returns the tangent of X.

6. date object. This object can store any date, from 0001 to 9999, and can be precise to the number of milliseconds (1/1000 seconds ). Internally, a date object is an integer, which is the number of milliseconds from, January 1, January 1, 1970 to the date indicated by the date object. If the date is earlier than January 1, 1970, it is a negative number. If no time zone is specified for all date and time zones, the "UTC" (Universal Time) Time Zone is used. It is the same as the "GMT" (Greenwich Mean Time) in numerical values.
Define a date object:
VaR d = new date;
This method makes d a date object and has an initial value: current time. To customize the initial value, you can use:
VaR d = new date (99, 10, 1); // October 1
VaR d = new date ('oct', 1999 '); // January 1, October 1
And so on. The best way is to use the "method" described below to strictly define the time.
Method
There are many methods like "g/set [UTC] XXX", which indicate both the "getxxx" method and the "setxxx" method. "Get" is to get a value, while "set" is to set a value. If it contains the "UTC" letter, it indicates that the obtained/set value is based on the UTC time. If it does not, it indicates the default time based on the local time or browsing period.
If no description is provided, the format of the method is <Object>. <method>, the same below.
G/set [UTC] fullyear () returns/sets the Year, which is represented by four digits. If "x. Set [UTC] fullyear (99)" is used, the year is set to January 1, 0099.
G/set [UTC] year () returns/sets the year, expressed by two digits. When this parameter is set, the browser automatically starts with "19". Therefore, "x. Set [UTC] year (00)" is used to set the year to 1900.
G/set [UTC] month () returns/sets the month.
G/set [UTC] Date () returns/sets the date.
G/set [UTC] day () returns/sets the week, 0 indicates Sunday.
G/set [UTC] hours () return/set hours, in the 24-hour format.
G/set [UTC] minutes () returns/sets the number of minutes.
G/set [UTC] seconds () returns/sets the number of seconds.
G/set [UTC] milliseconds () returns/sets the number of milliseconds.
G/settime () returns/sets the time, which is the internal processing method of the date object. The number of milliseconds from on January 1, January 1, 1970 to the date specified by the date object is calculated. If you want to delay the time specified by a date object by one hour, use: "x. settime (X. gettime () + 60*60*1000); "(60 minutes per hour, 60 seconds per minute, 1000 milliseconds per second ).
Gettimezoneoffset () returns the number of minutes in which the date object uses the time zone and Greenwich Mean Time Difference. The value is negative in the city of Greenwich Mean East. For example, if the value is China Time Zone (GMT + 0800), "-480" is returned ".
Tostring () returns a string that describes the date specified by the date object. The format of this string is similar to: "Fri Jul 21 15:43:46 UTC + 0800 2000 ".
Tolocalestring () returns a string that describes the date specified by the date object, in the local time format. For example, "15:43:46 ".
Togmtstring () returns a string that describes the date specified by the date object, in GMT format.
Toutcstring () returns a string that describes the date specified by the date object, in UTC format.
Parse () usage: Date. parse (<date Object>); returns the internal expression of the date object.

7. Global Object
The global object is invisible. It can be said that it is virtualized to "objectize" the global function ". In the Microsoft JScript Language Reference, it is called a "global object". The method and attribute used to reference it does not use "Global. XXX" (this will cause an error) and uses "XXX" directly ".
Attribute
Nan.
Method
Eval () runs strings in parentheses as standard statements or expressions.
Isfinite () returns true if the number in the brackets is "Limited" (between number. min_value and number. max_value); otherwise, false.
Isnan () if the value in the brackets is "Nan", true is returned; otherwise, false is returned.
Parseint () returns the value after converting the content in the brackets to an integer. If it is a string in parentheses, the digits starting with the string are converted into integers. If it starts with a letter, "Nan" is returned ".
Parsefloat () returns the value after converting the string in parentheses to a floating point number. The numeric part starting with the string is converted to a floating point number. If it starts with a letter, "Nan" is returned ".
Tostring () usage: <Object>. tostring (); converts an object to a string. If a value is specified in parentheses, all values are converted to a specific hexadecimal value during the conversion process.
Escape () returns the new encoded string from the brackets. This encoding is applied to the URL, that is, the space is written in the format of "% 20. "+" Is not encoded. If "+" is required, use escape ('...', 1 ).
Unescape () is the inverse process of escape. The string in the brackets is a general string.

8. A function is a method of returning objects or objects.
Function types
Common functions include constructor, such as array (), which can be used to construct an array, global functions, methods in global objects, and custom functions.
Custom Functions
Use the following statement to define a function:
Function Name ([parameter set]) {
...
[Return [<value>];]
...
}
The braces used after the function and the end of the function cannot be omitted, even if the function has only one sentence.
Function names and variable names have the same naming rules, that is, they only contain letters, numbers, underscores (_), letter headers, and cannot be repeated with reserved words.
The parameter set is optional, but must contain parentheses.
A parameter is a bridge between the function and the function. For example, if you want a function to return 3 cubes, you must let the function know the value "3, at this time, a variable is required to receive a value, which is called a parameter.
A parameter set is a set of one or more parameters separated by commas, such as A, B, and C.
The function has at most one row of statements, which are not executed immediately, but only executed when other programs call it. These statements may contain "return" statements. When a function is executed, the Return Statement is met, and the function is immediately stopped and returned to the program that calls it. If "return" is followed by a <value>, this value is returned when you exit the function.
In a function, parameters can be directly used as variables and VAR statements can be used to create new variables. However, these variables cannot be called by external functions. To enable the internal information of a function to be called externally, either the return value or the global variable must be used.
The global variable defined in the "Var" statement of the script's "root" (not inside the function) is a global variable, which can be called or changed anywhere in the process.
Example
Function addall (a, B, c ){
Return A + B + C;
}

VaR Total = addall (3, 4, 5 );
In this example, a function named "addall" is created. It has three parameters: A, B, and C. The function returns the result of adding three numbers. Outside the function, use "Var Total = addall (3, 4, 5);" to receive the return value of the function.
In more cases, functions do not return values. Such functions are called "procedures" in some languages that emphasize strict requirements, for example, "sub" in basic language and "procedure" in PASCAL Language ".
Attribute
Arguments is an array that reflects the parameters specified when an external program calls a function. Usage: Call "arguments" directly within the function ".

9.

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.