Object-based JavaScript language

xiaoxiao2021-03-06  56

(Li Xiaohua April 29, 2001) 01:50) JavaScript language is object-based (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 their own objects as needed to further expand the application range of JavaScript, enhance the multi-write web documentation. 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 used to be triallyly, and the following ways can be obtained: O reference JavaScript internal objects; o is provided by the browser environment; o Creates 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: O This statement is the 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. o The advantage of this statement is that you can operate without knowing the number of properties in the object. Example: The following functions are the contents of the display:

Function showdata (object) for (VAR x = 0; x <30; x ) Document.write (Object [i]); this function is to access the properties of each object by array, using this way First, you must know the quarantine value of the array, otherwise an error will occur if it exceeds the range. To make the for ... in statement, you don't need to know the number of object properties at all, see below:

Function ShowData (Object) for (VAR PROP IN 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) WITH statement means: In this statement, any reference to variables 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) THIS keyword this is a current reference, in JavaScript, because of the reference to the object, multi-directional, often reference to an object, the reference 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) New 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" University.date = 1999 "Where university is an existing object, name, city, date is the three properties of it, and assigns it by operation. (2) Implementation reference through the subject's subscript

UNIVERSITY [0] = "Yunnan" University [1] = "Kunming" university [2] = "1999" through an array form of access attributes, 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 do it after the number of its properties:

Function Showmy (Object) for (VAR PROP IN THIS) DOCAMENT.WRITE (3) Implemented by strings

University ["Name"] = "Yunnan" University ["City"] = "Date" "" 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 o String object: internal staticity. O When accessing Properties and Methods, you can use the (.) Operator. O Basic usage 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: o Anchor Anchor (): This method creates the same Anchor tag as in the HTML document. Use Anchor as in HTML (A Name = "). Access from the following format: string.Anchor (Anchorname). o Control Method for Character Display BIG font display, italics () oblique word display, bold () bold word display, blink () character flashes, small () characters Display, fixed () fixed high bright words Display, FontSize (size) control font size, etc.

o Font color method; FontColor (color) o String Size Transfer Toolcase () - Calcup conversion, TouPpercase () uppercase conversion. The following sends a given string to uppercase and lowercase formats respectively: string = stringvalue.touppercase and string = stringvalue.tolowercase. O Character Search: Indexof [Charactor, fromNDEX] starts searching for the first appearance of Charactor from the specified FormINDTX position. A part string of the back string: substring (start, end) returns all the characters 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. Get date time method getYear (): Return the number of years getMonth (): Return to the number of months GetDay (): Return to the number of days number getDay (): Return to the week getHours (): Return to the hour GetMintes (: Return minutes getseconds (): Return to the number of seconds GetTime (): Return milliseconds (2) Setting date and time: setYear (); set the year setDate (): Set the month Number SetMonth (): Sets the number of monthsSetHours (): Sets the number of minutes setSeconds (): Set the number of seconds setTime (): Set the number of milliseconds ... 2, the system function in JavaScript JavaScript The system function is also known as an internal method. It provides a system function-independent of any object, using these functions without creating any instance, 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: unscape String) 3. Returns the code of the character: method name: escape (character) 4. Return to the real number: parsefloat (floustring); 5. Returns different credits: PARSEINT (Numbestring, Rad.x) where Radix is ​​the number of applications, the number of NUMBS strings, the following is the JavaScript document displayed by a clock. Use a lot of functions in the document. TEST4_1.HTM