Object-Oriented Programming
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_valueUsage: number. min_value; returns the "minimum value ".
NanUsage: number. Nan or Nan; "Nan" is returned ". "Nan" (not a numerical value) was introduced very early.
Negative_infinityUsage: number. negative_infinity; Return Value: negative infinity, smaller than the "minimum" value.
Positive_infinityUsage: 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 ...).
Ln2Returns the natural logarithm of 2 (LN 2 ).
Ln10Returns the natural logarithm of 10 (ln 10 ).
Log2eReturns the logarithm of low E (log2e ).
Log10eReturns the logarithm (log10e) of E with a lower value of 10 ).
PiReturns π (1415926535 ...).
Sqrt1_2Returns the square root of 1/2.
Sqrt2Returns 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
NanI 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.
Function
Function 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 ".