Object-oriented JavaScript language

xiaoxiao2021-03-06  63

Most JavaScript developers use JavaScript as a Script language, and don't realize that JavaScirpt has a powerful object-based function. In fact, JavaScript can support the perfect object-oriented programming (PARADIGMS), many practical JavaScript controls It is an object development based on JavaScript. This article will describe how to achieve encapsulation, inheritance, and polymorphism in JavaScipt.

1. Object Object Object Object is the basic object of JavaScript, which contains Object objects in any other JScript object, which is available in all other objects. It can be considered that Object is the base class for all JavaScript classes. A method of overloading the Object class in a user-defined object, for example, a Tostring method is an example of frequent overload. Since it is a prototype-based interpreted language, JavaScript allows you to add any more properties to an object at any time, this point and C , C is a strong type of compilation language, all object methods and attributes must First define it. Example 1: obj = new object; obj2 = new object; obj.x = 1; // Add attribute XOBJ.Y = 2 to OBJ; // Add attributes to OBJ Y He He, OBJ's attribute X and Y is our additional addition The two properties, X, Y is attached to OBJ, and there is no x, y, in OBJ2, two properties.

2. The structure of the object JavaScript allows the custom type, each instance of the type has the properties defined inside the type. The implementation of the type is to be directly implemented by the definition function, each function can be directly seen as a class, and must use the New key when generating an object. Example 2: function foo () {this.x = 1; this.y = 2;} Obj1 = new foo; Obj2 = new foo;

Any of the objects constructed according to FOO contains x, y attributes.

2. Prototype Properties The prototype property returns a reference to the object type prototype. All jscript inherent objects have a read-only Prototype property that can add functions to the prototype of a JScript inherent object, but cannot give the object other prototype. User-defined objects can be given other prototypes.

For example, a method of adding a largest element value in an array is to an Array object. To complete this, declare this function, add it to Array.Prototype and use it.

Function array_max () {var i, max = this [0]; for (i = 1; i

3. Method JavaScript in the class JavaScript allows the method to add a method to the added attribute so that the implementation of the method can be encapsulated, and the caller does not require the specific implementation of the method.

Function foo () {this.x = 1; this.y = 2; this.bar = mymethod;} function mymethod (z) {this.x = z;} Obj2 = new foo;

Now we can call functions like this: obj2.bar (3); the implementation of the above is not very convenient, using the Prototype property simpler implementation class.

Function foo () {this.x = 1;} function foo.prototype.doot () {this.x ;} obj = new foo; obj.doot ();

4. Inheritance implementation We can achieve inheritance through Prototype. This is to note that the constructor of the base class is only called once. If each generated object needs to be called, you can use 5 Alternative method.

// Base class definition function textObject (st) {this.st = st; this.fvisible = true;}

// Write method in the base class function.Prototype.write () {document.write ('' this.st);}

// Subclass definition Function ItalicTextObject (st) {this.st = st;}

// Continuously link the base class and subcatencibject.prototype = new textObject ('x') by overwriting prototype;

// Subclass method defines italictextObject.prototype.write = itowrite; function itowrite () {document.write ('' this.st '');}

Obj1 = new textObject ('Hello, MOM'); Obj2 = New ItalicTextObject ('Hello, World'); Obj1.write (); Obj2.write ();

5. Another inheritance replacement

Define a method deriveFrom, which is used to connect the base class and subclass.

Function function.prototype.deriveFrom (fnbase) {var prop; if (this == fnbase) {Alert ("ERROR - Cannot Derive from self"; return;} for (prop in fnbase.prototype) {ix (typeof (fnbase) .prototype [prop]) == "function" &&! this.prototype [prop]) {this.prototype [prop] = fnbase.prototype [prop];}} this.prototype [fnbase.stname ()] = fnbase; }

Function function.prototype.stname () {var st; st = this.tostring (); st = st.substring (st.indexof (") 1, st.indexof (")) Return st;}

// Base class definition function textObject (st) {this.st = st; this.fvisible = true;}

// Base class WRITE method function textObject.Prototype.write () {document.write ('' this.st);}

// Base class isvisible method function textObject.prototype.isvisible () {Return this.fvisible;} // SubdextObject (st) {this.TextObject (st);}

// Continuously link the base class and subcaters to ItalicTextObject.deriveFrom (TextObject) through the DeriveFROM function call;

// Sub-class WRITE method function itAlicTextObject.prototype.write () {document.write ('' this.st '');}

Obj1 = new textObject ('Hello, MOM'); Obj2 = New ItalicTextObject ('Hello, World'); Obj1.write (); Obj2.write ();

6. Polymorphic implementation JAScript is not a strong type of language, we can achieve polymorphisms by defining the same name:

Function foo () {this.x = 1; this.doit = ​​foomthod;} function foomethod () {this.x ;} function bar () {this.z = 'hello'; this.doit = ​​barthod;} function barmethod () {THIS.Z = this.z;} Obj1 = new foo; Obj2 = new bar;

// Polymorphism example function poly (obj) {obj.doot ();} poly (obj1); poly (obj2);

7. Constructor Property The constructor property is a member of all objects with prototype, saves a reference to a function of constructing a specific object instance. You can use the object constructor attribute to determine the type of object.

Example: x = new string ("hi"); if (x.constructor == String) // processes (the condition is true). Or Function myfunc {// function body. }

Y = new myfunc; if (y.constructor == myfunc) // is processed (the condition is true).

(Last Update: 2004-12-08, to be limited ...)

转载请注明原文地址:https://www.9cbs.com/read-83973.html

New Post(0)