JavaScript getting started tutorial (3) js object-oriented

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

JavaScript uses "Object-Oriented Programming" or "Object-Oriented Programming. The so-called "Object-Oriented Programming" refers to dividing the scope of JavaScript into large and small objects. objects are still divided into objects until they are very detailed. All programming is based on objects, object-based. A variable or webpage document, window, or screen is an object. This chapter describes how JavaScript runs.
Basic object knowledge
ObjectIt is a small piece of JavaScript "sphere of influence". It can be a text, an image, a Form, and so on. Each object has its ownAttribute,MethodAndEvent. ObjectAttributeIs to reflect some specific properties of the object, such as: the length of the string, the length and width of the image, text in the text box (Textbox), and so on; objectMethodCan do some things on this object, such as form "Submit" and window "rolling ".EventYou can respond to the events that occur on the object. For example, you can submit a form to generate a "Submit event" and click "Click Event" generated by the connection ". Not all objects have the preceding three properties. Some have no events, and some have only attributes. For any property of the referenced object, use the method <Object Name>. <property name>.

Basic Object
Now we have to review what we have learned-to relearn some data types from the perspective of objects.
NumberNumber object. This object is rarely used and has never been seen by the author at one time. However, there are more objects belonging to "Number", that is, "variables.

Attribute
MAX_VALUEUsage: Number. MAX_VALUE; returns "maximum value ".
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 '.

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

Attribute
LengthUsage: <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. Note: One character in a string is 0th characters, the second character is 1st characters, and the last character is length-1 characters.
CharCodeAt ()
Usage: <String object>. charCodeAt (<position>); returns the ASCII code of a single character whose position is <position>.
FromCharCode ()
Usage: String. fromCharCode (a, B, c...); returns a String. the ASCII code of each character of this String is determined by a, B, c.
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 previous position 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 character>); returns an array separated from <String object>. <delimiter character> determines the location of the separator, it is not included in the returned array. For example, '1 & 2 & 345 & 678 '. split (' & ') returns an array: 1, 2, and 345,678. We will discuss the array in a moment.
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 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.
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.

ArrayArray 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 (since it is a "position", it also starts from scratch ).
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>] = ...;

Note that square brackets do not mean "can be omitted". The subscript representation of the array is enclosed by 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! Do not use the stupid method "Array (3, 4)" to define a 4x5 Two-dimensional Array, or use the "myArray [2, 3]" method to return the elements in the "two-dimensional array. For any call in the form of "myArray [..., 3]", only "myArray [3]" is returned. To use multi-dimensional arrays, use this virtual method:

Var myArray = new Array (),...);

In fact, this is a one-dimensional array, and each element inside it is an array. When calling the "two-dimensional array" element: myArray [2] [3] = ...;

Attribute
LengthUsage: <array Object>. length; returns the length of the array, that is, the number of elements in the array. It is equal to the subscript plus one of the last element 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>]) to sort 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. <Method functions> it is difficult to describe. Here we will only introduce some useful <method functions>.
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-".
For more information about functions, see the following.

MathIt is a mathematical object that provides mathematical computation of data. The attributes and methods mentioned below do not detail "usage". Remember to use the format "Math. <Name>" when using it.

Attribute
EReturns 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 low e (log2e ).
LOG10E
Returns the logarithm (log10e) of e with a lower value of 10 ).
PI
Returns π (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)
Return 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 (ex) of e ).
Floor (x)
Returns the largest integer less than or equal to x.
Log (x)
Returns the natural logarithm (ln x) of 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.

DateDate 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, 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 ()
Returns/sets the number of 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, 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. 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.

Global Object
The global object is invisible. It can be said that it is virtualized to "objectize" the global function ". In Microsoft JScript Language Reference, it is called a "Global object", but the methods and attributes that reference it never use "Global. xxx "(In addition, this will cause errors), and" xxx "is used directly ".

Attribute
NaN said it early in the morning.
Method
Eval ()Run the string in parentheses as a standard statement or expression.
IsFinite ()
If the Number in the brackets is "Limited" (between Number. MIN_VALUE and Number. MAX_VALUE), true is returned; otherwise, false is returned.
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 in 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.

FunctionFunction Definition
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, custom functions, and so on.
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.
ParametersIt 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 is used orGlobal Variables.
Global VariablesThe variable defined by the "var" statement in the Script's "root" (not inside the function) is a global variable, which can be called and 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
ArgumentsAn array that reflects the parameters specified when an external program calls a function. Usage: Call "arguments" directly within the function ".

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.