A summary of methods and attributes of JavaScript common objects _javascript tips

Source: Internet
Author: User
Tags lowercase natural logarithm numeric value square root string methods
This article will briefly describe the properties and methods of some common objects in JavaScript, as well as several useful system functions.
   a series of methods
  
JavaScript has a powerful string-processing function, with these series of methods, in order to write a colorful Web page. In this section, we will describe how to use the methods and properties associated with a string object.
   1. Length property of String objects
  
The string object has only one attribute length, which represents the relative number of characters that the string contains. The syntax is: stringname.length
   2. Methods of String objects
  
JavaScript provides a number of string methods to help control display information. String methods can be divided into several large classes, such as property methods, navigation methods, and so on. Below we will introduce the classification.
   (1) Property methodThe string's property method specifies how normal text is displayed on the screen. It provides an ideal way to make the homepage more appealing.
The Blink () Blink () method causes the text to flicker, in the following syntax: String.blink ();
The Bold () bold () method causes the string to become bold. The grammatical forms are as follows: String.bold ();
The FontColor () FontColor () method can change the color of a single string, or change the color each time a page or event occurs. Similarly, this method also has a corresponding HTML identifier--the syntax of FontColor () is string.fontcolor (color); In this case, color is a legitimate representation of a color.
The italics () italics () method turns the string into italics to differentiate it from the rest of the text, and the syntax for italics () is as follows: String.italics ();
   (2) Casing MethodJavaScript strictly distinguishes the case of letters. To easily compare two strings, convert them to uppercase or lowercase. The casing method of a String object supports conversion between uppercase and lowercase letters.
The toLowerCase () toLowerCase () method converts each character in a given string to a lowercase state. The grammatical forms are as follows: String=stringvalue.tolowercase ();
The toUpperCase () is the toUpperCase () method, which converts a given string to an uppercase string instead of the toLowerCase () method. String=stringvalue.touppercase ();
   (3) Operation MethodThe action method provides a number of ways to find strings within a text string, including Charat (), Indexo f (), LastIndexOf (), and substring () four methods. We can use these methods to format and manipulate the string variables entered by the user, where Charat () and substring () extract one or more characters from a string, and the remaining two methods return the numeric value where the first character of the value to find appears in the string.
The CharAt () charAt () method returns the character at the specified position from a given string. It is used in the following ways: String.charat (index); Where index is a number between 0 and less than the length of a string (the string length can be obtained by using length property),
The substring () substring () method returns one of its substrings from a large string. Its grammatical form is as follows: String.substring (INDEXA,INDEXB) Indexa and INDEXB are both numeric values between 0 and string lengths. They are the two subscript of the string.
The IndexOf () indexOf () method returns the position of a substring that matches the specified string in a long string. It looks for the given string from left to right, and the syntax is as follows: String.IndexOf (Searchvalue,[fromindex]); Where string is the indexof () in which to look for the Searchvalue string, and Searc Hvalue is any character or substring that you want to find. Fromindex is an optional option that specifies where to start the lookup. The range of Fromindex is similar to the Charat () and Subs Tring () (0 to Length-1). It cannot be greater than or equal to length, and if Fromindex is not specified, the method considers it by default and finds the entire string. The return value of IndexOf () is the first character offset (from the beginning of the string) that Searchvalue first appears. Even if Fromindex is specified, the return value is still relative to the beginning of the string (the SE archvalue that may appear in the previous places will be ignored).
The LastIndexOf () LastIndexOf () method is similar to the indexof () method, except that it looks for strings from right to left. LastIndexOf () still gives the first character position of the searched string, not the end character position. The syntax for the LastIndexOf () method is as follows: String. Last IndexOf (Searchvalue); Searchvalue is still the string you want to find.
   (4) Navigation methodThe concepts of anchor and link are exposed when using hypertext. Anchors are like labels placed on a Web page (you can easily go back there), and links are guidelines for another page (or anchor for the same and different pages).
The anchor () anchor () method is used to set an anchor whose syntactic form is simple: String.anchor (anchorname); Examine the following example: Var indexstring= "Index"; Document.writeln (Indexstring.anchor ("Anchorname")); This statement prints the text on the screen index;anchorname is the real name of this anchor. Anchors like this are generally used by links to jump to the actual position of the anchor in the page.
The function of link () Link () is to jump to the specified target. It corresponds to a standard HTML reference (a hypertext reference specified by the href attribute). Its grammatical form 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 legitimate URL reference. The following example shows how to use link () to create a link that jumps directly to the anchor created earlier; var indexstirng= "index" Document.writeln (Indexstring.link ("#" + "Anchorname") + "") This statement prints the link-formatted text Index on the screen (usually underlined below) Anchorname is the actual name of the anchor. When you click this link, the anchor appears at the top of the browser user window. Note: Be sure to add a # number to the anchor reference within the same document. When you add a #, enclose it in double quotes and connect it with a full reference with a plus sign. You can see the previous example. If you omit the # number, the link's target is a file rather than an anchor. Clicking on this chain causes the browser to attempt to mount the file named Anchorname (in this case). Because this file does not exist, the browser will generate NotFound error messages.
Second, the window method
A window is an interface that the browser interacts with the user. You can create a controllable window by using the window methods and properties in JavaScript. In this section, you will describe some of the window methods and properties.
   1. Predefined Windows (dialog box)
JavaScript provides three methods to produce a "simple" window (or dialog box): Alert (), confirm (), and Promp t (). Although we cannot control the location, button, or size of these dialog boxes, they provide a preliminary mechanism to prompt the user or receive input. The methods are discussed below.
The alert () alert () method establishes a separate message window with the following syntax: alert ("message") where "message" is a string of text to be displayed in the prompt window.
The Confirm () confirm () method is used to request a user's approval before an event is made. It gives the user the opportunity to stop an event from appearing. The syntax of the method is as follows: Confirm ("message") where the message is a string of text.
The prompt () prompt () method receives feedback from users who can type information from the keyboard or if they receive a default value, which can be selected with the mouse. The syntax of the method is as follows: Prompt ("message") if you want to specify a default value, you can use: prompt ("message", Imputdefault) where "message" is text or a number. Users can type any information. Therefore, a variable may be entered as text if it wants to enter a number for calculation. If you click the Cancel key and the close key, PR ompt () returns a null value.
   2. Custom Windows
The above three kinds of windows are simple, but they have a common disadvantage that they are inflexible, that is, they cannot change their size and style. Next, let's discuss how to customize the window in JavaScript. First, we should know how to create a window.
The Open () open () method creates a new window. Creating a window using this method is similar to the effect of selecting the new Web Bro wser for the File menu item. The syntax of the method is as follows: Windowvar=window.open ("url", "Windowname"), or you can define a window's properties: 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 invoke when the current new window is created. If no URL is specified (using the empty string ""), then the window is empty. Windowname represents the name of the window. Windowfeatures are optional attributes that can be assigned to a window, such as a toolbar, position, table of contents, status bar, menu bar, scroll bar, size variable capacity, width and height (see appendix). Set them as delimiters with commas and double quotes at the top.
Window properties These properties are used in the following form: Feeture=value where feature is the property to be selected, value (except width and height two attributes) or Yes (or 1) to activate the property, or no (or 0) to eliminate it. The syntax for width and height is as follows: feature=pixels where pixels is any positive integer (or positive integer variable), determines the size (in pixels) of the new window.
Close () uses the closed () method, which is syntactically as follows: Windowreference.close () You can use any of the following functions to turn off the current window: Window.close (), Self.close (), and closing (). We can close a separate window by referencing the Windowvar of a window, which is generated when the open () method is invoked. For example: When a window is created, the commands used are: Windowb=open (",", "Anotherwindow", "windth=80,height=40"); We can close it in any other window, using the command: Windowb.close () to close the topmost window can be top.close () of course, if you want to close the current window, we can use any of our own shutdown commands.

JavaScript provides us with some very useful common internal objects and methods. Users do not need to use their scripts to implement these features. This is the real purpose of object-based programming.
A string (string), math (numeric) and date (date) three objects and other related methods are provided in JavaScript. This provides a very favorable condition for programmers to quickly develop powerful scripting programs.

1. Common internal objects
For references to object properties and methods in JavaScript, there are two scenarios: one is that the object is a static object, that is, you do not need to create an instance for it when referencing the object's properties or methods, while another object or method that references it must create an instance of it, that is, the object is a dynamic object.
A reference to a JavaScript inner object so that it is tightly centered around its properties and methods. Therefore, it is very important to understand the static motion of the object for mastering and understanding the inner object of JavaScript.

1), String objects
O String object: internal static.
o When accessing properties and methods, you can use the (.) operator to implement.
o Basic use Format: Objectname.prop/methods
(1) The properties of the string object
The object has only one property, that is, length. It shows the number of characters in the string, including all the symbols.
Cases:
Mytest= "This is a JavaScript"
Mystringlength=mytest.length
The last Mystringlength returns the length of the mytest string at 20.
(2) Methods of String objects
There are 19 methods for a string object. Used primarily for display of strings in Web pages, font size, font color, character search, and character capitalization conversions.
The main methods are as follows:
O Anchor Point anchor (): This method creates the same anchor tag as in an HTML document. Use anchor as in HTML (A name= ""). Accessed through the following format: String.anchor (anchorname).
O Control method for character display
Big font display, italics () italic display, Bold () bold character display, blink () character flashing display, small () characters in small text display, fixed () fixed highlight word display, fontsize (size) control font size.
o font color method; fontcolor (color)
o String Case Conversion
toLowerCase ()-Lowercase conversion, touppercase () uppercase conversion. The following converts a given string to uppercase and lowercase respectively:
String=stringvalue.touppercase and String=stringvalue.tolowercase.
o character search: Indexof[charactor,fromindex]
Searches for the first occurrence of charactor from the specified FORMINDTX location.
Returns part of a string: substring (start,end)
All characters from start to end are returned.
 
2), arithmetic function of the math object
Function: Provides an introduction to the Readme operation except addition, subtraction, multiplication and addition. such as logarithm, square root and so on.
Static motion: Static Object
(1) Main properties
The math provides 6 properties that are often used in mathematics as constants E, 10-based natural logarithm LN10, 2-based natural logarithm LN2, 3.14159 Pi, and 1/2 square roots sqrt1-2,2 of the square root of SQRT2.
(2) Main methods
Absolute value: ABS ()
Sine cosine value: sin (), cos ()
Inverse cosine of sine: ASIN (), ACOs ()
Tangential tangent: tan (), Atan ()
Rounded: Round ()
Square root: sqrt ()
Value based on several square times: Pow (base,exponent)
...
3), date and Time object
Function: Provides an object about the date and time.
Static: Dynamic, that is, you must use the new operator to create an instance. Cases:
Mydate=new Date ()
The Date object does not provide direct access to the property. Only have methods to get and set dates and times.
Date Start value: January 1, 1770 00:00:00.
1. Time method to get date
GetYear (): Number of years returned
GetMonth (): Returns the month's numbered
GetDate (): Back to the date of the numbered
Getday (): Back to the day of the week
GetHours (): Returns the number of hours
Getmintes (: Back to number of minutes
Getseconds (): Returns the number of seconds
GetTime (): Returns the number of milliseconds
(2) Set date and time:
Setyear (); Set year
Setdate (): Set the month's numbered
Setmonth (): Set the number of copies of the month
Sethours (): Set Number of hours
Setmintes (): Set Number of minutes
Setseconds (): Set Number of seconds
SetTime (): Set the number of milliseconds
...
2. System functions in JavaScript
The system functions in JavaScript are also called internal methods. It provides system functions that are independent of any object and can be used directly without creating any instances.
1. Returns the value in a string expression:
Method Name: eval (string expression), example:
Test=eval ("8+9+5/2");
2. Return string ASCI code:
Method Name: Unescape (String)
3. Returns the encoding of the character:
Method Name: Escape (character)
4. return real number:
Parsefloat (floustring);
5. Returns the number of different systems:
parseint (Numbestring, Rad. X
Where Radix is the number of the binary, numbs string number

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.