JavaScript Reference Tutorials _ Basics

Source: Internet
Author: User
Tags addall numeric local time lowercase natural logarithm numeric value square root types of functions
JavaScript Reference Tutorials
JavaScript is used as "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.
Basic knowledge of objects
An object is a small piece of text, a picture, a form, and so on that can be separated from the JavaScript "sphere of influence." Each object has its own properties, methods, and events. The properties of an object reflect certain properties of the object, for example, the length of the string, the width of the image, the text in the text box (textbox), and so on, the object's method can do something about the object, such as "submit" the form, "scrolling" (scrolling) of the window, and so on , and the object's events can respond to things that happen to the object, such as submitting the form's "Submit event," 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--learn some data types from an object perspective.

Number "Numeric" 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.


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.


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

String String object. The simplest, quickest, most efficient, and most common way to declare a string object is to assign a value directly.


The length usage:< the String object >.length; returns the lengths of the string.


CharAt () uses:< string Object >.charat (< position >), and returns the single character of the string at < 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 () uses:< string Object >.charcodeat (< position >), and 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 () Use:< string Object >.indexof (< Another string object >[, < starting position]), which looks for < another string object > from < string object > (if given < start position > ignore previous position), if found, return its position, return "1" without finding it. All the "locations" are zero-based.
LastIndexOf () uses:< 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 < string object > by the,< delimiter character >, which determines where the separation is. It is not itself included 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 (< Beginning >[, < end); Returns the substring of the original string, which is the original string from < to > position to < end > A section of the previous position of a 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 () uses:< string Object >.substr (< start >[, < long); Returns a substring of the original string that starts at < > position and is a section of the < length >. 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 () uses the:< string Object >.tolowercase (), and returns a string that turns all uppercase letters of the original string to lowercase.
toUpperCase () uses:< string Object >.touppercase (); Returns a string that capitalizes all lowercase letters of the original string.

An 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] = ...;


Length usage:< 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] = ....


Join () Usage:< Array Object >.join (< delimiter >); Returns a string that strings each element in the array and uses < delimiter > to place the element with the element. 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 the array [1, 2, 3] uses this method, it will make it into: [3, 2, 1].
Slice () uses:< 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.
The sort () usage:< the Array object >.sort ([< method function), and arranges 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.

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.


E returns 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 (LOG2E) with a low of 2.
LOG10E returns the logarithm of E (log10e) with a low of 10.
Pi returns PI (3.1415926535 ...).
Sqrt1_2 returns the square root of 1/2.
SQRT2 returns the square root of 2.


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 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.


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, which is represented by 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 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, which is the internal processing method of a Date object: The number of milliseconds from January 1, 1970 from the beginning of the calculation to the date 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 the 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 format.
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 "" (and so it goes wrong) and are used directly with "XXX".


NaN said it a long time ago.


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.

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.

A parameter is a bridge in which a function transmits information externally 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", which is called a variable to receive the value.

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 information inside a function to be called externally, either return a value using "returns", or use a global variable.

A variable defined by the "var" Statement of a global variable at the root of the Script (not within the function) is a global variable that can be invoked and changed anywhere in the entire process.


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.


Arguments an array that reflects the arguments specified when an external program calls a function. Usage: Call "arguments" directly inside 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: 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.