JavaScript is used in object-oriented programming, or object-oriented programming. The so-called "object-oriented programming", meaning that JavaScript can be involved in the scope of the division into large and small objects, objects continue to divide the object until very detailed, all the programming is object-oriented, object-based. Small to a variable, large to the Web page documents, windows and even screens, are objects. This chapter will "object oriented" to describe the operation of JavaScript. The basic knowledge of the
object
Object is a small chunk of the JavaScript "sphere of influence" that can be a piece of text , a picture, a form (form), and so on. Each object has its own properties , methods , and events . The properties of an object reflect certain properties of the object, such as the length of the string, the width of the image, the text in the text box (Textbox), and so on; the method of the object can do something about the object, for example, The form's submit, the window's scrolling (scrolling), and so on, and the event of an object can respond to events that occur on the object, such as submitting the form to produce a "submit event" for the forms, and clicking on the "click event" generated by the connection. Not all objects have the above three properties, some have no events, some have only attributes. Any of the "properties" of the Reference object are in the method of "< object name >.< property name >".
basic objects
Now we're going to go over what we've learned--to get some data types back to the angle of the object. The
number "Number" object. This object is rarely used, and the author has not seen it once. But the object that belongs to "number", that is, "variable" is much.
Property
max_value Usage: number.max_value, return to maximum.
Min_value usage: number.min_value; Returns the minimum value.
Nan usage: Number.NaN or nan; returns "Nan". "NaN" (not a numerical value) was introduced very early.
Negative_infinity usage: number.negative_infinity; returns: Negative infinity, a value smaller than the minimum value.
Positive_infinity usage: number.positive_infinity; returns: Positive infinity, a value larger than the maximum value.
Method
toString () uses:< numeric variable >.tostring (); returns: A numeric value in the form of a string. such as: if a = = 123, then a.tostring () = = ' 123 '.
StringThe string object. The simplest, quickest, most efficient, and most common way to declare a string object is to assign a value directly.
Property
lengthUsage:< String Object >.length; Returns the length of the string.
Method
charAt ()Usage:< String Object >.charat (< position >); Returns the single character of the string at the < position > bit. Note: one character in the string is the No. 0 digit, the second is the 1th, and the last character is the length-1 bit.
charCodeAt ()Usage:< String Object >.charcodeat (< position >); Returns the ASCII code of a single character with the string at < position > bit.
fromCharCode ()Usage: String.fromCharCode (A, B, c ...) ; Returns a string with the ASCII code for each character by a, B, c ... And so on to determine.
IndexOf ()Usage:< String Object >.indexof (< Another string object >[, < starting position]) This method looks for < Another string object > from < string object > (if you give the < start position > Ignore previous position), if found, return to its position, not found, return "1". All the "locations" are zero-based.
LastIndexOf ()Usage:< String Object >.lastindexof (< Another string object >[, < start position); like IndexOf (), it's just looking from behind.
Split ()Usage:< String Object >.split (< delimiter character >); Returns an array that is separated from < string object > in the,< delimiter character > determines where the separation is and is not contained in the returned array. For example: ' 1&2&345&678 '. Split (' & ') returns an array: 1, 2,345,678. As for the array, we'll discuss it in a moment.
SUBSTRING ()Usage:< String Object >.substring (< start >[, < final); Returns a substring of the original string that is the previous position of the original string from the < to > position to the end of < position. < end >-< Start > = Returns the length of the string. If you do not specify < end > or specify more than the length of the string, the substring is taken from the < start > position to the end of the original string. If the specified location cannot return a string, an empty string is returned.
SUBSTR ()Usage:< String Object >.substr (< start >[, < long); Returns a substring of the original string that starts at < > position and is the length of the < long >. If you do not specify < long > or specify more than the length of the string, the substring is taken from the < start > position to the end of the original string. If the specified location cannot return a string, an empty string is returned.
toLowerCase ()Use:< String Object >.tolowercase (); Returns a string that turns all uppercase letters of the original string to lowercase.
toUpperCase ()Use:< String Object >.touppercase (); Returns a string that capitalizes all lowercase letters of the original string.
ArrayAn array object. An array object is a collection of objects that can have different types of objects. Each member object of an array has a subscript that represents its position in the array (since it is "position" and is zero-based).
How the array is defined:
var < array name > = new Array ();
This defines an empty array. To add an array element later, use:
< array name >[< subscript] = ...;
Note that the brackets here are not "omitted", and that the subscript notation for the array is enclosed in square brackets.
If you want to initialize the data directly when you define the 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 myarray, inside the element is: myarray[0] = = 1; MYARRAY[1] = = 4.5; MYARRAY[2] = = ' Hi '.
However, if there is only one element in the list of elements, and the element is a positive integer, this defines an array containing the < positive integers > empty elements.
Note: JavaScript has only one-dimensional arrays! Never use the stupid method of "array (3,4)" to define a two-dimensional array of 4 x 5, or use the "myarray[2,3" method to return elements in a two-dimensional array. Any call in this form of "myarray[...,3]" actually returns only "Myarray[3". To use a multidimensional array, use this virtual method:
var myarray = new Array (new Array (), new Array (), new Array (
), ...);
In fact, this is a one-dimensional array, each element inside is an array. Call this "two-dimensional array" of elements: myarray[2][3] = ...;
Property
lengthUsage:< Array Object >.length; return: The length of the array, that is, how many elements are in the array. It equals the subscript plus one of the last element in the array. So, want to add an element, only need: myarray[myarray.length] = ....
Method
Join ()Usage:< Array Object >.join (< delimiter >); Returns a string that strings each element of the array together and places the < delimiter > between the element and the element. This method does not affect the original contents of the array.
Reverse ()Use:< array Objects >.reverse (), so that the order of elements in the array is reversed. If the array [1, 2, 3] uses this method, it will make it into: [3, 2, 1].
Slice ()Usage:< Array Object >.slice (< start >[, < final); Returns an array that is a subset of the original array, starting at <, and finally < at the end of >. If you do not give the < end, the subset is taken to the end of the original array.
Sort ()Use:< Array Object >.sort ([< method function)), so that the elements in the array in a certain order. If the < method function is not specified, it is sorted alphabetically. In this case, 80 is ahead of the 9 row. If the < method function is specified, the sort method specified by the < method function > is sorted. < method function > more difficult to tell, here are only a few useful < method functions > introduced to you.
Sort numbers in ascending order:
function SortMethod (A, b) {
return a-b;
}
Myarray.sort (SortMethod);
Sort numbers in descending order: Put the above "a-b" into "b-a".
For functions, see below.
Math The Math object, which provides a mathematical calculation of the data. The properties and methods mentioned below do not specify "usage" any more, but remember to use the format "math.< name >" When used.
Property
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 the E (LOG2E) with a low of 2.
LOG10EReturns the logarithm of the E (log10e) with a low of 10.
PIReturns PI (3.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 inverse cosine of x (the angle of cosine equal to X), expressed in radians.
ASIN (x)Returns the inverse chord value of x.
Atan (x)Returns the tangent value of x.
atan2 (x, y)Returns the amplitude of the complex number corresponding to the interior point (x, y) of the complex plane, expressed in radians, whose value is between-π and π.
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 of x (ln x).
Max (A, b)Returns the larger number in a, B.
Min (A, b)Returns the smaller number in a, B.
Pow (n, m)Returns the M-Power (nm) of N.
Random ()Returns a random number that is greater than 0 or less than 1.
Round (x)Returns the value after the X is rounded.
Sin (x)Returns the sine of x.
sqrt (x)Returns the square root of x.
Tan (x)Returns the tangent of x.
Date The Date object. This object can store any date, from 01 to 9999, and can be accurate to milliseconds (1/1000 seconds). Internally, the Date object is an integer, which is the number of milliseconds from the beginning of January 1, 1970 to the date that the Date object refers to. If the date indicated is earlier than 1970, then it is a negative number. All datetime, if no time zone is specified, takes a "UTC" (World time) time zone, which is the same as the value of GMT (GMT).
Define a Date object:
var d = new Date;
This method makes D a Date object and already has an initial value: the current time. If you want to customize the initial values, you can use:
var d = new Date (99, 10, 1); October 1, 99
var d = new Date (' Oct 1, 1999 '); October 1, 99
And so on, methods. The best approach is to use the "methods" described below to strictly define the time.
Method
Here are a number of "g/set[utc]xxx" methods, which indicate both "getxxx" and "setxxx" methods. "Get" is a numeric value, and "set" sets a value. If it has a "UTC" Letter, the obtained/set value is based on UTC time, and no representation is based on the local time or the browse period default time.
If no explanation, the use of the method format is: "< object >.< method >", hereinafter.
g/set[utc]fullyear ()Returns/sets the year, expressed in four digits. If you use "x.set[utc]fullyear (99)", the year is set to 0099 years.
G/set[utc]year ()Returns/sets the year, expressed in two digits. When set, the browser automatically adds "19" to the beginning, so use "X.set[utc]year (00)" To set the year to 1900 years.
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 means Sunday.
G/set[utc]hours ()Returns/sets the number of hours, 24-hour system.
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 that is the internal processing method of a Date object: The number of milliseconds from the date of January 1, 1970 that are beginning to be computed to the dates that the Date object refers to. If you want to postpone the time specified by a Date object by 1 hours, use: "X.settime (X.gettime () + 60 * 60 * 1000);" (60 minutes per hour, one minute 60 seconds, 1000 milliseconds a second).
getTimezoneOffset ()Returns the number of minutes between the time zone used by the Date object and GMT. In the urban area of Greenwich Oriental, the value is negative, for example: China time zone (gmt+0800) returns "-480".
ToString ()Returns a string that describes the date that the Date object refers to. The format of this string is similar to the following: "Fri June 15:43:46 utc+0800 2000".
toLocaleString ()Returns a string that describes the date that the Date object refers to, with the local time representation format. such as: "2000-07-21 15:43:46".
toGMTString ()Returns a string that describes the date that the Date object refers to, in GMT format.
toUTCString ()Returns a string that describes the date that the Date object refers to, in UTC.
Parse ()Usage: Date.parse (< Date object >); Returns the internal expression of the Date object.
Global Objects
Global objects never appear, it can be said to be virtual out, the purpose is to "object" global functions. In the Microsoft JScript language Reference, it is called the Global object, but the methods and properties that refer to it are never "global.xxx" (and so it goes wrong) and are used directly with "XXX".
Property
NaN said it a long time ago.
Method
eval () runs the string in parentheses as a standard statement or an expression.
Isfinite () returns True if the number in parentheses is "limited" (between Number.min_value and Number.MAX_VALUE), otherwise it returns false.
isNaN () returns True if the value in parentheses is "NaN" or returns false.
parseint () returns the value after converting the contents of parentheses into integers. If a string is inside the parentheses, the portion of the number at the beginning of the string is converted to an integer and, if it begins with a letter, returns "NaN".
parsefloat () returns the value of converting the string in parentheses to a floating-point number, which is converted to floating-point numbers at the beginning of the string, and "NaN" if preceded by a letter.
toString () uses the:< object >.tostring (), and converts the object to a string. If you specify a numeric value in parentheses, all the values in the conversion process are converted to a specific system.
Escape () returns the encoded new string of strings in parentheses. The encoding is applied to the URL, which is written as a "%20" format. "+" is not coded, if you want to "+" is also coded, please use: Escape (' ... ', 1).
unescape () is the reverse process of escape (). The string in the solution brackets becomes a generic string.
function
Definition of function
The so-called "function" is a method of an object or object that has a return value.
Types of Functions
Common functions are: constructors, such as Array (), that can construct an array, global functions, methods in global objects, custom functions, and so on.
Custom Functions
Define the function with the following statement:
function
name ([parameter set]) {
...
[return[< value];]
...
}
Among them, the curly braces that are used after the function and the end of functions cannot be omitted, even if the whole function is only one sentence.
The function name and variable name have the same naming rules, that is, only alphanumeric underline, letter at the queue, can not repeat with reserved words.
The set of parameters is optional, but the parentheses must be there.
ParametersIs the function of the external to the function of the transfer of information to the bridge, for example, to call a function to return 3 cubic, you have to let the function know the "3" This value, this time there will be a variable to receive the value, this variable is called parameters.
A parameter set is a collection of one or more parameters separated by commas, such as a, B, and C.
Within a function, there is a maximum line of statements that do not execute immediately, but only when other programs call it. These statements may contain a "return" statement. When a function is executed, the return statement is encountered, and the function immediately stops executing and returns to the program in which it was invoked. If "return" is followed by a < value, the value is returned while the function is exited.
Inside a function, parameters can be used directly as variables, and new variables can be created with the Var statement, but these variables cannot be invoked by procedures outside of the function. To enable the information inside the function to be called externally, either return the value using "returns", or use the
Global Variables。
Global VariablesThe variable defined in the "var" Statement of the Script's "root" (not inside the function) is a global variable that can be invoked and changed anywhere in the entire process.
cases
function AddAll (A, B, c) {
Return a + B + C;
}
var total = AddAll (3, 4, 5);
This example creates a function called "AddAll", which has 3 parameters: A, B, C, which returns the result of adding three numbers. Outside the function, use "var total = AddAll (3, 4, 5);" The return value of the receive function.
More often than not, the function has no return value, which is called "process" in some of the more rigorous languages, such as "Sub" of Basic class language, "procedure" of Pascal language.
The
property
arguments An array that reflects the parameters specified when the function is called by an external program. Usage: Call "arguments" directly inside the function.