Summary of methods and attributes of common JavaScript objects

Source: Internet
Author: User
Tags access properties color representation natural logarithm string methods

This article will briefly introduce the attributes and methods of some common objects in JavaScript, as well as several useful system functions.
  I. String Method
  
JavaScript has powerful string processing functions. With these string methods, you can compile a variety of web pages. This section describes how to use methods and attributes related to string objects.
  1. length attribute of the string object
  
The string object has only one attribute length, which indicates the relative number of characters contained in the string. Syntax: stringName. length
  2. Method of String object
  
JavaScript provides multiple string methods to help control display information. String methods can be divided into several categories, such as attribute methods and navigation methods. Next we will introduce the categories.
  (1) attribute MethodThe attribute method of a string specifies how normal text is displayed on the screen. It provides an ideal way to make the Home Page more attractive.
The blink () method causes text flashing. The syntax format is as follows: string. blink ();
The bold () method changes the string to bold. Syntax format: string. bold ();
The fontcolor () method can change the color of a single string, or change the color each time a page or event is reloaded. Similarly, this method also has a corresponding HTML identifier. the syntax of fontcolor () is string. fontcolor (color). Here, color is a valid color representation.
The italics () method converts strings into italics, which are separated from other words. the syntax of italics () is as follows: string. italics ();
  (2) Case sensitivityJavaScript strictly distinguishes uppercase and lowercase letters. To conveniently compare two strings, convert them to uppercase or lowercase letters. String objects can be converted between uppercase and lowercase.
The toLowerCase () tolowerCase () method converts each character in a given string to a lower-case state. Syntax format: string = stringValue. toLowerCase ();
The opposite of toUpperCase () and tolowerCase () is the toUpperCase () method, which converts a given string into an uppercase string. String = stringValue. toUpperCase ();
  (3) Operation MethodThe operation method provides many methods to search for strings in a text string, including charAt (), indexO f (), LastIndexOf (), and substring. We can use these methods to format and manipulate string variables entered by users. The charAt () and substring () Methods Extract one or more characters from a string, the remaining two methods return the numeric value where the first character of the value to be searched appears in the string.
The charAt () method returns the character at the specified position from a given string. Its usage is as follows: string. charAt (index); index is a number between 0 and less than the Length of the string (the Length of the string can be obtained through the Length attribute ),
The substring () method returns a substring of a large string. Its syntax is as follows: string. substring (indexA, indexB) indexA and indexB are both numerical values between 0 and the string length. They are two subscripts of a string.
The indexOf () method returns the position of the child string that matches the specified string in a long string. It looks for the given string from left to right. The syntax format is as follows: string. indexOf (searchValue, [fromIndex]); Where string is the string in which indexOf () is to be searched for searchValue, and searc hValue is any character or substring to be searched. FromIndex is an option that specifies the start position of the search. The fromIndex range is similar to charAt () and Subs tring () (0 to length-1 ). It cannot be greater than or equal to length. If fromIndex is not specified, this method considers it as the default? And search for the entire string. The return value of indexOf () is the offset of the first character that appears for the first time in searchValue (starting from the beginning of the string ). Even if fromIndex is specified, the returned value is still relative to the start of the string (the se archValue may appear in the previous sections and will be ignored ).
The lastIndexOf () method is similar to the IndexOf () method, except that it searches for strings from right to left. LastIndexOf () still gives the position of the first character of the string to be searched, rather than the position of the last character. The syntax of the lastIndexOf () method is as follows: string. Last indexOf (searchValue); searchValue is still the string to be searched.
  (4) Navigation MethodWhen hypertext is used, the concept of anchor and link is exposed. An anchor is like a bookmark placed on a webpage (you can easily return to it); a link is a guide to another page (or an anchor of the same and different pages.
The anchor () method is used to set an anchor. Its syntax is simple: string. anchor (anchorName); Evaluate the following example: var indexString = "Index"; document. writeln (indexString. anchor ("anchorName"); this statement prints the text Index on the screen; anchorName is the real name of the anchor. Such an anchor is generally used by links to jump to the actual location of the anchor in the page.
Link () is used to jump to a specified target. It is equivalent to standard HTML reference (hypertext reference specified by the HREF attribute ). Its Syntax format is also very simple: linkText. link (href) linkText is the text on the screen, the href attribute is not necessarily an anchor, it can also be a legal URL reference. The following example shows how to use link () to create a link directly jump to the previously created anchor; var indexStirng = "Index" document. writeln (indexString. link ("#" + "anchorName") + "") print the text Index of the link format on the screen (usually underlined below ); anchorName is the actual name of the anchorName. When you click this link, the anchor appears at the top of the browser user window. Note: ensure that the # number is added to the reference of the anchor in the same document. When adding the # sign, enclose it in double quotation marks and use the plus sign to connect it with the complete reference. See the previous example. If # is omitted, the link target is a file rather than an anchor. Clicking this link will make the browser try to mount the file named anchorName (in this example. Because this file does not exist, the browser will generate the Notfound error message.
Ii. Window Method
A window is the interface on which the browser interacts with the user. You can create a controllable window by using the window methods and properties in JavaScript. In this section, we will introduce some window methods and properties.
  1. pre-defined window (dialog box)
JavaScript provides three methods to generate a "simple" window (or dialog box): alert (), confirm (), and promp t (). Although we cannot control the positions, buttons, or sizes of these dialogs, they provide a preliminary mechanism to prompt users or receive input. The methods are discussed below.
The alert () method creates an independent message window. The syntax is as follows: alert ("message") Where "message" is the text string to be displayed in the prompt window.
The confirm () method is used to request user approval before an event is performed. It gives users the opportunity to stop an event. The syntax of this method is as follows: confirm ("message") Where message is a text string.
The prompt () method receives user feedback. You can enter information on the keyboard, or if they receive a default value, you can select with the mouse. The syntax of this method is as follows: prompt ("message") if you want to specify a default value, you can use: prompt ("message", imputDefault) Where "message" is a text or number. You can enter any information. Therefore, even if you want to enter a number for calculation, a variable may also be input as text. If you click the Cancel key and the close key, pr ompt () returns a null value.
  2. Custom window
Although the above three windows are simple, they have a common disadvantage that they are not flexible, that is, they cannot change their size and style. Next, we will discuss how to customize the window in JavaScript. First, we should know how to create a window.
The open () method creates a new window. Using this method to create a window is similar to selecting the New Web Bro solution of the File menu item. The syntax of this method is as follows: windowVar = window. open ("URL", "windowName") or you can define the properties of a window: windowVar = window. open ("URL", "windowName", "windowAttribut er") Where WindowName is the name of the window to be created. A URL is a text string that specifies the URL to be called when a new window is created. If no URL is specified (using an empty string ""), the window is empty. WindowName indicates the name of the window. WindowFeatures is an optional attribute that can be assigned to a window, such as a toolbar, location, directory, status bar, menu bar, scroll bar, variable size, width, and height (see the attached table ). When setting them, use a comma as the separator and add double quotation marks to the outermost part.
Window properties use the following attributes: feeture = value where feature is the property to be selected, value (except width and height) or yes (OR 1) to activate this property, or no (or 0) to remove it. The syntax of width and height is as follows: feature = pixels where pixels is any positive integer (or positive integer variable) to determine the size of the new window (in pixels ).
Close () uses the close () method. Its syntax is as follows: windowReference. close () can use any of the following functions to close the current window: window. close (), self. close (), close (). We can close an independent window by referencing windowVar of a window (this value is generated when the open () method is called. For example, the command used to create a window is export WB = open (",", "anotherwindow", "windth = 80, height = 40 ); we can close it in any other window and run the command: Export WB. close () to close the top-level window, you can use the following method to top. close () Of course, if you want to close the current window, you can use any self-closing command.

Javascript provides us with some very useful internal objects and methods. You do not need to use scripts to implement these functions. This is the real purpose of object-based programming.
In javascript, three methods are provided: string, math, and Date. This provides very favorable conditions for programmers to quickly develop powerful script programs.

1. Common internal objects
In javascript, object attributes and methods are referenced in two situations: one is that the object is a static object, that is, you do not need to create an instance for the object when referencing its properties or methods. The other object must create an instance for the object or method that references the object, that is, this object is a dynamic object.
References to javascript internal objects are closely centered around their attributes and methods. Therefore, it is of great significance to understand and understand the static nature of objects in javascript.

1) String object
O string object: internal static.
O you can use the (.) operator to access properties and methods.
O basic format: objectName. prop/methods
(1) attributes of string objects
This object has only one attribute, that is, length. It indicates the number of characters in the string, including all characters.
Example:
Mytest = "This is a javascript"
Mystringlength = mytest. length
Finally, mystringlength returns the length of the mytest string to 20.
(2) method of String object
There are 19 methods for string objects. It is mainly used for character string display on the Web page, font size, font color, character search, and Case conversion of characters.
The main method is as follows:
O anchor (): This method creates the same anchor tag as in the Html document. Use anchor as in Html (A Name =. Access in the following format: string. anchor (anchorName ).
O how to control character display
Big font, Italics (), Italics (), bold (), bold (), blink (), small (), fixed () fixed highlight display, fontsize (size) control font size, etc.
O fontcolor method; fontcolor (color)
O string case-insensitive Conversion
ToLowerCase ()-lower case conversion, and toUpperCase () upper case conversion. The following code converts a given string to uppercase or lowercase:
String = stringValue. toUpperCase and string = stringValue. toLowerCase.
O character search: indexOf [charactor, fromIndex]
Search for the location where the charactor first appears from the specified formIndtx position.
Returns a part of the string: substring (start, end)
All characters starting from start to end are returned.
 
2) math object of arithmetic functions
Function: provides some self-reported operations except addition, subtraction, multiplication, and division. Such as logarithm and square root.
Static: static object
(1) main attributes
Math provides six attributes, they are often used in mathematics E constant, base 10 natural logarithm LN10, base 2 natural logarithm LN2, 3.14159 PI, 1/2 square root SQRT1-2, the square root of 2 is SQRT2.
(2) Main Methods
Absolute Value: abs ()
Sine Cosine: sin (), cos ()
Arcsin arccosine: asin (), acos ()
Tangent arc tangent: tan (), atan ()
Round: round ()
Square Root: sqrt ()
Pow (base, exponent)
...
3) Date and Time object
Function: provides an object related to date and time.
Static: Dynamic. You must use the New operator to create an instance. Example:
MyDate = New Date ()
The Date object does not provide direct access attributes. Only date and time can be obtained and set.
Start Date: 00:00:00, January 1, January 1, 1770.
1. method for obtaining the date
GetYear (): returns the number of years.
GetMonth (): returns the number of the current month.
GetDate (): returns the number of the current day.
GetDay (): returns the day of the week.
GetHours (): returned hours
GetMintes (: returns the number of minutes
GetSeconds (): returns the number of seconds.
GetTime (): returns the number of milliseconds.
(2) set the date and time:
SetYear (); set year
SetDate (): set the number of the current month
SetMonth (): set the number of copies in the current month
SetHours (): set the number of hours
SetMintes (): set the number of minutes
SetSeconds (): set the number of seconds
SetTime (): set the number of milliseconds
...
2. system functions in javascript
System functions in javascript are also called internal methods. It provides system functions unrelated to any object. Using these functions does not require creating any instances and can be used directly.
1. Return the value in the string expression:
Method Name: eval (string expression), for example:
Test = eval ("8 + 9 + 5/2 ");
2. Return the string ASCI:
Method Name: unEscape (string)
3. encoding of returned characters:
Method Name: escape (character)
4. Return real numbers:
ParseFloat (floustring );
5. Return the numbers of different hexadecimal values:
ParseInt (numbestring, rad. X)
Here, radix is the number base, and the number of numbs strings

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.