JavaScript Getting Started 4 --------------- Jilin University CAE Campus Fee Forum Faith 2008

xiaoxiao2021-03-06  57

The JavaScript language is object-based (Object-based), rather than object-oriented. The reason why it is an object-based language, mainly because it does not provide many functions such as abstraction, inheritance, overloading and other object-oriented languages. Instead, the complex objects created by other languages ​​come together to form a very powerful object system. Although the JavaScript language is an object based, it still has some object-oriented basic features. It can create its own objects as needed to further expand the application range of JavaScript, enhance the preparation of powerful web text files. First, the basic knowledge of the object 1, the object's basic structure JavaScript is constituted by two basic elements of the Properties and Method (Methods). The former is an object to implement the loading unit of the information in the process of implementing it, thereby associating with the variable; the latter means that the object can be performed in accordance with the designer's intention, thereby associating a particular function. 2. The path to the reference object is to be truly used, and the following ways can be obtained: refer to the JavaScript internal object; provided by the browser environment; create a new object. This is to say that the object must exist before being referenced, otherwise the reference will be meaningless, and an error message will appear. From above we can see that the JavaScript reference object can be obtained in three ways. Either create new objects or use existing objects. 3. About object operation statement JavaScript is not a pure object-oriented language, which provides many functions for object-oriented languages, so JavaScript designers provide it with you "Object" rather than object-oriented language, in JavaScript Several statements and keywords and operators used to operate objects. 1), for ... in statement format is as follows: for (Object Attribute Name IN Known Object Name) Description: The functionality of this statement is a control loop for operating all attributes of known objects. It is set to a variable all attributes of a known object; rather than using counters. The advantage of this statement is to operate without having to know the number of properties in the object. Example: The following function is the content displayed in the display: Function Showdata (Object) for (VAR X = 0; x <30; x ) Document.write (Object)

); This function is to access the properties of each object by array, using this method, first of all, the following method must first be known, otherwise an error occurs if it is out of the range. To make the for ... in statement, you don't need to know the number of object properties. See below: Function ShowData (Object) Document.write (Object [Prop]); when using this function, In the cyclic body, the FOR automatically takes out the properties until the end is this. 2), the WITH statement means that in this statement, any reference to the variable is considered to be the properties of this object to save some code. WITH OBJECT {...} All statements in the curly brackets after the with statement are the field of the Object object. 3) HIS Keywords this is the current reference, in JavaScript, due to multi-level, multi-directional, often references to another object, and another object may be referenced Another object, which may cause confusion, and finally I don't know the object that is now referenced, and this JavaScript provides a statement THIS for specifying the object to specify the current object. 4) EW operator Although the function of the object in JavaScript is very powerful. But more powerful is that designers can create their own objects as needed to meet a particular requirement. Use the New operator to create a new object. It creates objects using the following format: newobject = new object (parameters table); where new object created by NewObject: Object is an object already existing; parameter Table parameter table; New is the command statement in JavaScript. If you create a date new object newData = new data () Birthday = new data (DecEmber 12.1998) can make NewData, Birthday as a new date object. 4. References of the reference object attribute of the object attribute can be implemented by one of the following three modes: (1) Use points (.) Operator university.name = "" university.city = "in Yunnan Province 1999 "Where university is an existing object, name, city, date is the three properties of it, and assigns it by operation. (2) Implementation of the subject's subscript implementation reference university [university [1] = "Kunming" university [2] = "1999" through the access properties in the array form, you can use a loop operation to obtain its value.

Function Showunievsity (Object) for (VAR J = 0; J <2; J ) Document.write (Object [J]) If you use for ... in, you can implement: Function Showmy after the number of properties: Function Showmy Object) for (VAR Prop In this) DOCAMENT.WRITE (3) Implementing the University [""] = "Yunnan"] = "City"] = "Kunming" university ["DATE"] = "1999" 5, the reference to the object's method is very simple to reference in the JavaScript. Objectname.Methods () actually Methods () = functionname method is essentially a function. If you reference the showmy () method in the university object, you can use: Document.write ()) or: Document.write (university), such as cos () in the Math internal object: with (MATH) Document.write (COS (35)); Document.write (COS (80)); If you do not use with a reference, relatively complex: Document.write (Math.cos (35)) Document.write (Math.sin (80)) Second, the properties and methods of common objects JavaScript provide us with some very useful interiors and methods. Users don't need to implement these features with scripts. This is based on the true purpose of object programming. In JavaScript, String provides three objects, and Date (date) three objects and other related methods. Thus providing a very favorable condition for programmers to develop a strong script program. 1. There are two cases of reference to object properties and methods in JavaScript, and there are two cases: which is said that this object is a static object, that is, when references the object or method of the object, it is not necessary to create instances for it; An object is referenced to its object or method to create an instance for it, ie the object is a dynamic object. The reference to the JavaScript internal object is tight around its attributes and methods. Thus, the staticity of the object is very important for mastery and understanding the internal object of JavaScript. 1), string object String object: internal staticity. When accessing Properties and Methods, you can use the (.) Operator. Basic use format: Objectname.Prop / Methods (1) The properties of the string object This object has only one attribute, ie Length. It indicates the number of characters in the string, including all symbols. Example: mytest = "this is a javaScript" mystringLength = mytest.length Last MyStringLength Returns the length of 20 of the MyTest string. (2) The method of string object String object has 19 methods. Mainly used in the display, font size, font color, character's search, and upper case switching of characters in the web page. The main methods are as follows: Anchor Anchor (): This method creates the same Anchor tag as in the HTML text file.

Use Anchor as in HTML (A Name = "). Access from the following format: string.Anchor (Anchorname). The control method of the character display is displayed. Italics () bolding word display, bold () bold word display, the blink () character flashes, small () characters Display, fixed () fixed highlight display Fontsize (size) control font size, etc. Font color method; FontColor string case switching toLowerCase () - lowercase conversion, TouPercase () uppercase conversion. The following sends a given string to uppercase and lowercase formats respectively: string = stringvalue.touppercase and string = stringvalue.tolowercase. Character search: indexof [Charactor, fromNDEX] Start searching for the first appearance of Charactor from the specified FormINDTX position. Returns a string of strings: substring (start, end) returns all the characters starting from Start to END. 2) MATH object function of the arithmetic function: Provide a substantial self-proportioned operation other than plus, minus, multiplication. Such as logarithm, square root, etc. Static object: Static object (1) The main property Math provides 6 properties, which are often used in mathematics E, with a natural logarithm LN10 in the bottom of 10, with a natural logarithm LN2 of 2. 3.14159 of the PI, 1/2 square root SQRT1-2, 2 square root of SQRT2. (2) The main method is absolute: ABS () sinusoidal cosord value: sin (), cos () anti-sinine anti-surpass: asin (), acos () orthosis Anster: Tan (), Atan () Since 4: Round () Square root: SQRT () Based on a few-party values: POW (Base, Exponent) ... 3), date and time object function: Provide a date and time object. Staticness: Dynamic, you must create an instance using the New operator. Example: MyDate = new date () Date object does not provide properties that directly access. Only methods for obtaining and setting date and time. Date starting value: January 1, 1770 00:00. (1) Get date time method getYear (): Return to the number of years getMonth (): Return to the number of months getDAY (): Return to the day of the day gethours (): Return to the hour number getmintes (: Return minutes) GetSeconds (): Return to the number of secondtime (): Return milliseconds (1) Setting date and time: setYear (); set the year setDate (): Set the number of month number setMonth (): Set the number of months section setHours (): Set an hour Number SetMintes (): Sets the number of minutes setseconds (): Sets the number of seconds settime (): Sets the number of milliseconds ... 2, the system function in JavaScript JavaScript is also known as the internal method. It provides unrelated to any object System functions, using these functions do not need to create any instances, can be used directly.

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 code: method Name: Escape (character) 4) Return to the real number: parsefloat (floustring); 5) Return to different credits: parseint (NumbEString, Rad.x) where radix is ​​number The number of NUMBS strings is three, and the example below is a clock display JavaScript document. There is a lot of functions in the text file.

Test4_1.htm