JavaScript Reference Tutorial

Source: Internet
Author: User
Tags addall local time natural logarithm square root types of functions

JavaScript is either "Object-programmed" or "object-oriented programming." The so-called "object-oriented programming" means that the scope of JavaScript can be divided into large and small objects, objects continue to be divided under the object until very detailed, all the programming is based on the object as the starting point. As small as a variable, as large as a Web page document, window, or even screen, are objects. This chapter describes the "object-oriented" operation of JavaScript. Basic knowledge of objects

An object is a piece of text, a picture, a form (form), and so on that can be separated from the JavaScript "sphere of influence". Each object has its own properties , methods , and events . An object's properties reflect certain properties of the object, such as the length of the string, the width of the image, the text in a text box (Textbox), and so on, and the object's methods can do something about the object, for example, the form's "submit" (Submit), The window's "scrolling" (scrolling), and so on, and the object's events can respond to things that occur on the object, such as submitting a form to form the "commit event", click on the connection resulting from the "click event." Not all objects have the above three properties, some have no events, and some have only attributes. Refer to any of the "properties" of the object with the "< object name >.< Property name >" method.

Base Object

Now we're going to review what we've learned--to re-learn some data types from an object's perspective.

Number The "Number" object. This object is used very little, and the author has not seen it once. However, the "number" of the object, that is, "variable" is more.

Property

max_value usage: number.max_value; returns the maximum value.
Min_value
usage: number.min_value; Returns the minimum value.
Nan
usage: Number.NaN or nan; returns "NaN" (not a numeric value) introduced very early.
Negative_infinity
usage: number.negative_infinity; returns: Negative infinity, which is smaller than the minimum value.
Positive_infinity
usage: number.positive_infinity; returns: Positive infinity, which is larger than the maximum value.

Method

toString () usage:< numeric variable >.tostring (); returns: A numeric value in the form of a string. such as: if a = = 123; then a.tostring () = = ' 123 '.

String Object. Declaring a String object is the simplest, fastest, most efficient and common method of assigning values directly.

Property

Length Usage:< 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 of the characters in the string is No. 0, the second is the 1th bit, and the last character is the length-1 bit.
charCodeAt ()
Usage:< String Object >.charcodeat (< position >), returns the ASCII code of the single character of the string at the < position > bit.
fromCharCode ()
Usage: String.fromCharCode (A, B, c ...) ; Returns a string that is the ASCII code of each character by a, B, c ... Wait to be sure.
IndexOf ()
Usage:< String Object >.indexof (< Another string object >[, < start position;]); This method finds < Another string object from < String object > > (if the < start position is given > ignores the previous position, and if found, returns to its position and returns "1" without finding it. All "positions" are zero-based.
LastIndexOf ()
Usage:< String Object >.lastindexof (< Another string object >[, < start position;]), similar to IndexOf (), but looking from behind.
Split ()
Usage:< String Object >.split (< delimiter character >); Returns an array that is separated from the < string object >,< delimiter character > determines the place of separation, which itself is not included in the returned array. For example: ' 1&2&345&678 '. Split (' & ') returns the array: 1, 2,345,678. As for the array, we'll discuss it in a moment.
SUBSTRING ()
Usage:< String Object >.substring (< >[, < end;]); Returns the substring of the original string, which is a paragraph from the previous position of the original string from < > position to < final > position. < end >-< Start > = Returns the length of the string (length). If you do not specify < end > or specify more than the string length, the substring is always 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 (< >[, < long;]); Returns the substring of the original string, which is a section of the original string starting at < Start > position, at < length >. If you do not specify < long > or specify more than the string length, the substring is always 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 ()
Usage:< string Object >.tolowercase (); Returns a string that turns all uppercase letters of the original string into lowercase.
toUpperCase ()Usage:< string Object >.touppercase (); Returns a string that turns all lowercase letters of the original string into uppercase.

Array object. An array object is a collection of objects that can be of different types. Each member object of an array has an "subscript" that represents its position in the array (since it is "position", which is also zero-based).

How to define arrays:

var < array name > = new Array ();

This defines an empty array. To add an array element later, use:

< array name >[< subscript;] = ...;

Note that the square brackets here are not meant to be omitted, and the subscript representation of an array is enclosed in square brackets.

If you want to initialize the data directly when you define an array, use:

var < array name > = new Array (< element 1>, < element 2>, < element 3> ...);

For example, var myArray = new Array (1, 4.5, ' Hi '); An array myArray is defined, and the elements inside are: myarray[0] = = 1; MYARRAY[1] = = 4.5; MYARRAY[2] = = ' Hi '.

However, if there is only one element in the list of elements, and this element is a positive integer, this defines an array containing < positive integers > empty elements.

Note: JavaScript has only one-dimensional arrays! Do not use "array (3,4)" as a foolish way to define a two-dimensional array of 4 x 5, or "myarray[2,3" method to return the elements in a "two-dimensional array". Any "myarray[...,3" call in this form 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, and each element inside is an array. When invoking this "two-dimensional array" element: myarray[2][3] = ...;

Property

Length Usage:< Array Object >.length; return: The length of the array, which is the number of elements in the array. It equals the subscript of the last element in the array plus one. So, to add an element, just need: myarray[myarray.length] = ....

Method

join () usage:< Array Object >.join (< delimiter >); Returns a string that strings the elements in the array, with < delimiter > placed between elements and elements. This method does not affect the original contents of the array.
Reverse ()
uses the:< array Object >.reverse (), and the order of elements in the array is reversed. If this method is used for arrays [1, 2, 3], it will make the array: [3, 2, 1].
Slice ()
usage:< Array Object >.slice (< >[, < end;]); Returns an array that is a subset of the original array, starting with < start, and finally < final >. If you do not give < end, the subset is always taken to the end of the original array.
The sort ()
usage:< Array Object >.sort ([< method function;]); The elements in the array are arranged in a certain order. If the < method function is not specified, the alphabetical order is arranged. In this case, 80 is more than 9 before the row. If the < method function is specified, the sorting method is sorted by the < method function > specified. < method functions > more difficult to tell, here are only a few useful < method functions > introduced to everyone.

Sort numbers in ascending order:

function SortMethod (A, b) {
return a-B;
}

Myarray.sort (SortMethod);

Sort the numbers in descending order: the "A-B" above should be "b-a".

For functions, see below.

Math "Mathematical" object that provides mathematical calculations of the data. The properties and methods mentioned below no longer describe "usage" in detail, so remember to use the "math.< name >" format when you are using it.

Property

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 of 2 (LOG2E).
log10e
returns the logarithm of E with a low of 10 (log10e).
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 inverse cosine of x (the cosine value equals the angle of x), expressed in radians.
Asin (x)  
returns the inverse sine value of x.
Atan (x)  
returns the inverse tangent value of x.
atan2 (x, y)  
returns the amplitude angle of the complex number corresponding to the point (X, y) in the complex plane, expressed in radians, with a value 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 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 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 greater than 0 and less than 1.
Round (x)  
returns the value after 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 Day object. This object can store any one date, from 01 to 9999, and can be accurate to milliseconds (1/1000 seconds). Internally, the Date object is an integer that is the number of milliseconds from January 1, 1970 when it is beginning to be calculated to the date that the Date object refers to. If the indicated date is earlier than 1970, then it is a negative number. All datetime, if no time zone is specified, takes the UTC (world time) time zone, which is the same value as GMT.

Define a Date object:

var d = new Date;

This method makes D a Date object and has an initial value: the current time. If you want to customize the initial value, 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 forth method. The best way is to use the "method" described below to define time strictly.

Method

Here are a number of "g/set[utc]xxx" methods, which represent both the "GetXXX" approach and the "setxxx" approach. "Get" is a numeric value, and "set" is a value set. If you have a "UTC" Letter, the obtained/set value is based on UTC time, and none indicates the default time based on local time or browse period.

If no description is used, the format of the method is: "< object >.< method >", below.

g/set[utc]fullyear ()Returns/sets the year, expressed as a four-digit number. 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 double-digit numbers. Set the browser to automatically add "19" Start, so use "X.set[utc]year (00)" 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 represents 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, which is the internal processing method of the Date object: The number of milliseconds from January 1, 1970 to the date on which the Date object refers. If you want to postpone the time specified by a Date object by 1 hours, use: "X.settime (X.gettime () + 60 * 60 * 1000);" (one hour 60 minutes, one minute 60 seconds, 1000 milliseconds a second).
getTimezoneOffset ()
Returns the number of minutes that the date object takes in the time zone and GMT. In downtown Greenwich, this 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 Jul 15:43:46 utc+0800 2000".
toLocaleString ()
Returns a string that describes the date that the Date object refers to, using local time to represent the 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 format.
Parse ()
Usage: Date.parse (< Date object >); Returns the internal representation of the date object.

Global Objects

The global object is never visible, it can be said to be virtual, the purpose is to "object" the global function. In the Microsoft JScript language Reference, it is called a "Global object", but the methods and properties that refer to it are never "global.xxx" (and this would be an error) and use "XXX" directly.

Property

I said it early.

Method

eval () runs the string in parentheses as a standard statement or an expression.
Isfinite ()
returns True if the number within the parentheses is "limited" (between Number.min_value and Number.MAX_VALUE), otherwise false.
IsNaN ()
returns True if the value inside the parentheses is "NaN", otherwise false is returned.
parseint ()
returns the value after converting the contents of parentheses into integers. If the parenthesis is a string, the number part at the beginning of the string is converted to an integer, and if it starts with a letter, "NaN" is returned.
parsefloat ()
returns the value after the string in parentheses is converted to a floating-point number, the numeric part at the beginning of the string is converted to a floating-point, or "NaN" if it starts with a letter.
toString () uses the:< object >.tostring () and converts the object to a string. If you specify a numeric value in parentheses, all values in the conversion process are converted to a specific binary.
Escape () returns the string in parentheses after the encoded new string. The code is applied to the URL, that is, the space is written as "%20" in this format. "+" is not encoded, if you want to "+" is also encoded, please use: Escape (' ... ', 1).
unescape () is the anti-process of escape (). The bracketed string becomes a generic string.

function

Definition of a function

A "function" is a method of an object or object that has a return value.

Types of functions

Common functions are: constructors, such as array (), can construct an array, a global function, a method in a global object, a custom function, and so on.

Custom functions

Define the function with the following statement:

function name ([parameter set]) {
...
[return[< value;]
...
}

Among them, the curly braces after function and the end of functions cannot be omitted, even if the whole function has only one sentence.

The function name has the same naming as the variable name, that is, it contains only alphanumeric underlines, the letter of the line, and cannot be duplicated with the reserved word.

The set of parameters is optional, but the parentheses must be there.

A parameter is a bridge that transmits information from outside the function to a function, for example, to call a function to return a 3 cubic, you have to let the function know the value of "3", and then there is a variable to receive the value, which is called the parameter.

A parameter set is a collection of one or more parameters separated by commas, such as: A, B, C.

The inside of a function has a maximum line of statements that do not execute immediately, but only when other programs call it. These statements may contain "return" statements. When a function is executed, a return statement is encountered, and the function stops executing immediately and returns to the program that called it. If the "return" is followed by a < value, the value is returned at the same time that the function exits.

Inside the function, parameters can be used directly as variables, and variables can be created with the Var statement, but these variables cannot be called by procedures outside the function. To make the information inside the function be called externally, either return the value with "return" or use a global variable .

Global variables the variable defined by the "var" statement in the "root" (non-function) of the Script is a global variable that can be called and changed anywhere in the process.

Cases

function AddAll (A, B, c) {
Return a + B + C;
}

var total = AddAll (3, 4, 5);

This example establishes 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, the function has no return value, which is called "process" in some of the more rigorous languages, such as the "Sub" of the Basic language and the "procedure" of the Pascal language.

Property

arguments An array that reflects the parameters specified by the external program when the function is called. Usage: Call "arguments" directly inside the function.

JavaScript Reference Tutorial

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.