JavaScript language profile and simple instance

xiaoxiao2021-03-06  72

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 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 function is the content displayed in the array: 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 first know the quarantin value of the array, otherwise an error will occur 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 property 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 1999 "Where university is an existing object, name, city, date is the three properties of it, and assigns it by operation. (2) Reference university [0] = "Yunnan" university [1] = "1999" in the Nunan "University [1] =" 1999 "

By accessing 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 the number of its properties.

(3) Implementing the university ["Name"] = "Yunnan"] = "City"] = "Date" in Kunming "in the form of the string

5. The reference to the object's method is quite simple in 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 00:00. 1. Get date time method getyear (): Return to the number of days getMonth (): Return to the number of months getDay (): Return to the day GetHours (): Return the hour number getmintes (: Return minutes Getseconds (): Return to the number of secondtime (): Return to milliseconds (2) Setting date and time: setYear (); set the year setDate (): Set the number of month number setMonth (): Set the number of monthsSetHours (): Set the 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 a system-independent system Function, use these functions No 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. Return to the code: method name: escape (character) 4. Return to the real number: parsefloat (floustring); 5. Return different credited Number: PARSEINT (NUMBESTRING, RAD.X) where Radix is ​​the number of credits, the number of NUMBS strings, the following is a JavaScript document displayed by a clock. There is a lot of functions in the document.

Test4_1.htm