Basic knowledge of JavaScipt objects _ basic knowledge

Source: Internet
Author: User
Tags addall array definition time zones natural logarithm
Basic knowledge of JavaScipt objects 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
Objects are small pieces of JavaScript "sphere of influence". They can be a text, an image, a Form, and so on. Each object has its own attributes, methods, and events. The attributes of an object reflect some specific properties of the object, such as the length of the string, the length and width of the image, and the text in the Textbox; the object method can do some things on this object, for example, the form's "Submit", the window's "Scrolling", and so on; the event of the object can respond to the event that occurs on the object. For example, the "Submit event" of the form generated by submitting the form, 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. Any "nature" of the referenced object uses" <对象名> . <性质名> .

Basic Object
Now we have to review what we have learned-to relearn some data types from the perspective of objects.
Number "Number" 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_VALUE usage: Number. MAX_VALUE; "maximum value" is returned ".
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: <数值变量> . ToString (); returns a value in the string format. For example, if a = 123, a. toString () = '000000 '.

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

Attribute
Length usage: <字符串对象> . Length; returns the length of the string.
Method
CharAt () usage: <字符串对象> . CharAt ( <位置> ); Returns the string in <位置> A single character. 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: <字符串对象> . CharCodeAt ( <位置> ); Returns the string in <位置> The ASCII code of a single character.
FromCharCode () usage: String. fromCharCode (a, B, c ...); returns a string. the ASCII code of each character is composed of a, B, c... and so on.
IndexOf () usage: <字符串对象> . IndexOf ( <另一个字符串对象> [, <起始位置> ]); This method starts from <字符串对象> Search <另一个字符串对象> (If <起始位置> Ignore the previous position). If it is found, its position is returned. If it is not found, "-1" is returned ". All "positions" start from scratch.
LastIndexOf () usage: <字符串对象> . LastIndexOf ( <另一个字符串对象> [, <起始位置> ]); Similar to indexOf (), but it is located from the back.
Split () usage: <字符串对象> . Split ( <分隔符字符> ); Returns an array from <字符串对象> Separated, <分隔符字符> Determines the location of the separation, which 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: <字符串对象> . Substring ( <始> [, <终> ]); Returns the substring of the original string, which is <始> Location <终> The first part of the position. <终> - <始> = Returns the length of the string ). If no <终> Or if the specified length exceeds the string length <始> Position until the end of the original string. If the specified position cannot return a string, an empty string is returned.
Substr () usage: <字符串对象> . Substr ( <始> [, <长> ]); Returns the substring of the original string, which is <始> Start position, length: <长> . If no <长> Or if the specified length exceeds the string length <始> Position until the end of the original string. If the specified position cannot return a string, an empty string is returned.
ToLowerCase () usage: <字符串对象> . ToLowerCase (); returns a string that converts all uppercase letters of the original string into lowercase letters.
ToUpperCase () usage: <字符串对象> . ToUpperCase (); returns the string that converts all lowercase letters of the original string into uppercase letters.

Array 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 <数组名> = New Array ();

In this way, an empty array is defined. To add array elements later, use:

<数组名> [ <下标> ] = ...;

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 <数组名> = New Array ( <元素1> , <元素2> , <元素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 <正整数> Array of 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
Length usage: <数组对象> . Length; return value: 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: <数组对象> . Join ( <分隔符> ); Returns a string that concatenates all elements in the Array Using <分隔符> Place it between elements. This method does not affect the original content of the array.
Reverse () usage: <数组对象> . Reverse (); reverse the order of elements in the array. If you use this method for the array [1, 2, 3], it will make the array: [3, 2, 1].
Slice () usage: <数组对象> . Slice ( <始> [, <终> ]); Returns an array, which is a subset of the original array and starts from <始> , Finally <终> . If not <终> , The subset is always taken to the end of the original array.
Sort () usage: <数组对象> . Sort ([ <方法函数> ]); Arrange the elements in the array in a certain order. If you do not specify <方法函数> In alphabetical order. In this case, 80 is better than 9. If you specify <方法函数> , Then press <方法函数> Sort by the specified sorting method. <方法函数> It is hard to tell. Here we only use some useful <方法函数> This article will be introduced to you.
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.

Math is a mathematical object that provides mathematical computing for data. The attributes and methods mentioned below do not describe the "usage" in detail. Remember to use "Math. <名> .

Attribute
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 value of 2 (log2e ).
LOG10E returns the logarithm (log10e) of e with a low value of 10 ).
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 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) returns 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 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 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.

Date 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 as follows:" <对象> . <方法> ", The same below.
G/set [UTC] FullYear () returns/sets the Year, which is 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 () return/set 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 on 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 Difference. 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 ( <日期对象> ); 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 () runs strings in parentheses as standard statements or expressions.
IsFinite () returns true if the Number in the brackets is "Limited" (between Number. MIN_VALUE and Number. MAX_VALUE); otherwise, false.
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: <对象> . 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 from 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 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 [ <值> ];]
...
}

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.
A parameter 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 <值> , 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 or the global variable must be used.
The global variable defined in the "var" statement of the Script's "root" (not inside the function) is a global variable, which can be called or 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
Arguments is an 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.