Home > Article > Web Front-end > Summary of methods and attributes of common JavaScript objects_javascript skills
This article will briefly introduce the properties and methods of some commonly used objects in JavaScript, as well as several useful system functions.
1. String methods
JavaScript has powerful string processing functions. Only with these string methods can we write colorful web pages. In this section, we will introduce how to use the methods and properties related to string objects.
1. The length attribute of the string object
The string object has only one attribute, length. This attribute value indicates the relative number of characters included in the string. The syntax is: stringName.length
2. String object methods
JavaScript provides multiple string methods to help control the display of information. String methods can be divided into several major categories, such as attribute methods, navigation methods, etc. Below we will introduce the categories.
(1) Attribute method The string attribute method specifies the way ordinary text is displayed on the screen. It provides an ideal way to make your homepage more attractive.
blink() The blink() method causes the text to blink. The syntax is as follows: string.blink();
bold() The bold() method makes the string bold. The syntax is as follows: string.bold();
fontcolor() The fontcolor() method can change the color of a single string, or change the color every time the page is reloaded or an event occurs. Similarly, this method also has a corresponding HTML identifier - the syntax of fontcolor() is string.fontcolor(color); Here, color is a legal representation of a color.
italics() italics() method changes the string into italics to distinguish it from other text. The syntax of italics() is as follows: string.italics();
(2) Capitalization method JavaScript strictly distinguishes between upper and lower case letters. In order to easily compare two strings, they must be converted to uppercase or lowercase. The string object's case method supports conversion between upper and lower case.
toLowerCase() The tolowerCase() method converts each character in a given string to lowercase. The syntax is as follows: string=stringValue.toLowerCase();
toUpperCase() The opposite of the tolowerCase() method is the toUpperCase() method, which converts a given string into an uppercase string. string=stringValue.toUpperCase();
(3) Operation method The operation method provides many methods for searching strings in a text string, including charAt(), indexO f(), Four methods: LastIndexOf() and substring(). We can use these methods to format and operate string variables entered by the user. The charAt() and substring() methods extract one or more characters from a string, and the remaining two methods return the first value of the value to be found. The numeric value of the position at which characters appear in the string.
charAt() The charAt() method returns the character at the specified position from a given string. It is used as follows: string.charAt(index); where index is a number between 0 and less than the string length (the string length can be obtained through the Length attribute),
substring() substring() method from a Returns a substring of a larger string. Its syntax is as follows: string.substring(indexA, indexB) Both indexA and indexB are numeric values between 0 and the length of the string. They are the two subscripts of the string.
indexOf() The indexOf() method returns the position of the substring that matches the specified string in a long string. It searches the given string from left to right, and the syntax is as follows: string.indexOf(searchValue, [fromIndex]); where string is the string in which indexOf() wants to find searchValue, and searc hValue is whatever it wants to find. character or substring. fromIndex is an optional option that specifies the location where the search should begin. The range of fromIndex is similar to charAt() and Subs string() (0 to length-1). It cannot be greater than or equal to length. If fromIndex is not specified, this method assumes its default value and searches the entire string. The return value of indexOf() is the offset (from the beginning of the string) of the first character where searchValue first appears. Even if fromIndex is specified, the return value is still relative to the beginning of the string (se archValue that may appear in some previous places will be ignored).
lastIndexOf() The lastIndexOf() method is similar to the IndexOf() method, except that it searches the string from right to left. lastIndexOf() still gives the position of the first character of the searched string, not the position of the last character. The syntax of the lastIndexOf() method is as follows: string.Last indexOf(searchValue); searchValue is still the string you want to find.
(4) Navigation method When using hypertext, you will come into contact with the concepts of anchors and links. An anchor is like a bookmark placed on a web page (where you can easily return to it); a link is a guide to another page (or an anchor on the same or different page).
anchor() The anchor() method is used to set an anchor. Its syntax is very simple: string.anchor(anchorName); Consider 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. Anchors like this are typically used by links to jump to the anchor's actual location on the page.
link() The function of link() is to jump to the specified target. It is equivalent to a standard HTML quote (a hypertext quote specified by the HREF attribute).Its syntax 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 that jumps directly to the previously created anchor; var indexStirng="Index" document.writeln(indexString.link("#" "anchorName") " ") This statement Prints the text Index of the link format on the screen (usually underlined); anchorName is the actual name of the anchor. When this link is clicked, this anchor will appear at the top of the browser user window. Please note: Be sure to add a # sign to the reference of the anchor within the same document. When adding the # sign, enclose it in double quotes and connect it with the complete quote with a plus sign. See the previous example. If the # sign is omitted, the target of the link is a file rather than an anchor. Clicking on this link will cause the browser to attempt to load the file named anchorName (in this case). Because this file does not exist, the browser will generate a Notfound error message.
2. Window method
The window is the interface for the browser to interact with the user. Controllable windows can be created using window methods and properties in JavaScript. In this section, some window methods and properties will be introduced.
1. Predefined windows (dialog boxes)
JavaScript provides three methods to generate "simple" windows (or dialog boxes): alert (), confirm () and prompt ( ). Although we have no control over the position, buttons, or size of these dialog boxes, they provide a preliminary mechanism to prompt the user or receive input. Each method is discussed below.
alert() 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.
confirm() The confirm() method is used to request the user's approval before proceeding with an event. It gives the user the opportunity to stop an event from occurring. The syntax of this method is as follows: confirm("message") where message is a text string.
prompt() The prompt() method receives feedback from the user, who can type information from the keyboard or select with the mouse if they receive a default value. The syntax of this method is as follows: prompt("message") If you want to specify a default value, you can use: prompt("message", importDefault) where "message" is text or a number. Users can type any information. Therefore, although a variable expects a number to be entered for calculation, it may also be input to it text. If the Cancel key and the Close key are clicked, pr ompt() returns a null value.
2. Customized windows
Although the three windows above are very simple, they have a common disadvantage that they are inflexible, that is, their size and style cannot be changed. Next, let's discuss how to customize windows in JavaScript. First, we should know how to create a window.
open() The open() method will create a new window. The effect of creating a window using this method is similar to selecting the New Web Browser from the File menu item. The syntax of this method is as follows: windowVar=window.open("URL", "windowName") or you can also define the attributes of a window: windowVar=window.open("URL", "windowName", "windowAttribute") where WindowName is the name of the window to be created. URL is a text string that specifies the URL to be called when the current new window is created. If no URL is specified (use the empty string ""), 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, location, directory, status bar, menu bar, scroll bar, resizing capabilities, width and height (see attached table). Set them with commas as delimiters and outer double quotes.
Window attributes The usage form of these attributes is as follows: feeture=value where feature is the attribute to be selected, value (except for width and height attributes) is either yes (or 1) to activate the attribute, or no ( or 0) to eliminate it. The syntax of width and height is as follows: feature=pixels where pixels is any positive integer (or positive integer variable) that determines the size of the new window (in pixels).
close() Use the close() method, its syntax is as follows: windowReference.close() You 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 a window's windowVar (the value is generated when the open() method is called). For example: the command used when creating a window is: WindowB=open(",","anotherwindow","windth=80, height=40); We can close it in any other window using the command: windowB.close () To close the top-level window, you can use the following method top.close(). Of course, if you want to close the current window, we can use any self-closing command
JavaScript provides us with some very useful common internal objects. and methods. Users do not need to use scripts to implement these functions. This is the true purpose of object-based programming. JavaScript provides three objects: string (string), math (numeric calculation) and Date (date). Some other related methods.This provides very favorable conditions for programmers to quickly develop powerful script programs.
1. Commonly used internal objects
There are two situations when referencing object properties and methods in JavaScript: One is that the object is a static object, that is, when referencing the properties or methods of the object There is no need to create an instance for it; while another object must create an instance for it when referencing its object or method, that is, the object is a dynamic object.
The reference to the internal object of JavaScript is centered around its properties and methods. Therefore, clarifying the static nature of objects is of great significance for mastering and understanding the internal objects of JavaScript.
1), string object
o string object: internal staticity.
o When accessing properties and methods, you can use the (.) operator.
o Basic usage format: objectName.prop/methods
(1) Attributes of string objects
This object has only one attribute, length. It indicates the number of characters in the string, including all symbols.
Example:
mytest="This is a javascript"
mystringlength=mytest.length
Finally, mystringlength returns the length of the mytest string as 20.
(2) Methods of string objects
There are 19 methods in string objects. It is mainly used for the display of strings in Web pages, font size, font color, character search, and character case conversion.
The main methods are as follows:
o Anchor point anchor(): This method creates the same anchor tag as in the Html document. Use anchor as in Html (A Name=""). Accessed via the following format: string.anchor(anchorName).
o Control methods for character display
big font display, Italics() italic display, bold() bold font display, blink() character flashing display, small() character display in small font, fixed () Fixed highlighted word display, fontsize(size) controls font size, etc.
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]
Search for the first occurrence of character starting from the specified formIndtx position.
Return a part of the string: substring(start,end)
Return all characters from start to end.
2), math object of arithmetic function
Function: Provide some self-described operations except addition, subtraction, multiplication and division. Such as logarithms, square roots, etc.
Staticity: Static object
(1) Main attributes
Math provides 6 attributes. They are the constant E often used in mathematics, the natural logarithm LN10 with base 10, and The natural logarithm of base 2 is LN2, PI is 3.14159, the square root of 1/2 is SQRT1-2, and the square root of 2 is SQRT2.
(2) Main methods
Absolute value: abs()
Sine and cosine values: sin(), cos()
Arc sine and arc cosine: asin(), acos()
Tangent Arctangent: tan(),atan()
Rounding: round()
Square root: sqrt()
Value based on several powers: Pow(base,exponent)
...
3), Date and time objects
Function: Provide an object related to date and time.
Static and dynamic: Dynamic, that is, you must use the New operator to create an instance. Example:
MyDate=New Date()
Date object does not provide direct access properties. Only has methods to get and set date and time.
Date starting value: January 1, 1770 00:00:00.
1. Time method for getting 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 How many days
getHours(): returns the number of hours
getMintes(: returns the number of minutes
getSeconds(): returns the number of seconds
getTime(): returns the number of milliseconds
(2) Set date and time :
setYear(); set the year
setDate(): set the number of the current month
setMonth(): set the number of the current month
setHours(): set the number of hours
setMintes(): set 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 that are not related to any object. You do not need to create any instances to use these functions.
1. Return the value in the string expression:
Method name: eval (string expression) formula), for example:
test=eval("8 9 5/2");
2. Return the string ASCI code:
Method name: unEscape (string)
3. Return the character Encoding:
Method name: escape(character)
4. Return real numbers:
parseFloat(floustring);
5. Return numbers in different bases:
parseInt(numbestring,rad.X )
where radix is the base of the number, numbs is the string number