Object-oriented JavaScript programming

zhaozj2021-02-16  56

Title object-oriented JavaScript programming liuruhong (original) Keyword OOP, JavaScript, object-oriented

Object-oriented JavaScript programming

JavaScript should not be unfamiliar with the people who have done a Web program, the initial period is used to do some simple FORM verification, basically play some skillful things. IE 4.0 introduces DHTML, and in order to fight Netscape's JavaScript, I propose my own scripting language JScript, in addition to following EMAC standards, and increase many extensions, the OOP program to mention is one of them, for life Concept, JavaScript I mentioned below is JScript implemented above Microsoft Internet Explorer 4.0. For Netscape, I have not done too much programs, so some differences will be seen.

JavaScript is not an object-oriented language, more than a development platform, but JavaScript provides a very powerful prototype-oriented object-oriented call function, you can use them in your own needs. So, how to use objects? This article is as far as possible from JavaScript-oriented principle to analyze its working model. After you understand these models, you can write some implementation code in your own script, then call other places.

JavaScript syntax and C are very close, but do not use the keyword class in the class implementation, and there is no use of traditional public or implementation, etc., the so-called keywords are identified. In this case, someone may ask, how to write JavaScript Class, how to achieve inheritance. I haven't understood MSDN later, I know that prototype is adopted, including inheritance and overload, or through this keyword.

JavaScript's function is very strange, each is the default to implement optional, that is, the parameters can be optional, Function A (Var1, Var2, Var3), in the process of calling A (), A (Value1), A ( Value1, value2), etc. Call is correct, at least even if the compilation section can be fully passed, as other, just compare the function's implementation logic.

The implementation of the implementation, inheritance and overloading of JS is described in detail below.

1. achieve

The implementation of the JS class is directly implemented by the function. Each function can directly see the Class, as follows

Function classtest1 () {

... // Implement Code

}

Var a = new classtest1

Function classtest2 (var1) {

... // Implement Code

}

VAR B = New classtest ("Value")

For the properties of the class, you can implement two ways

1) THIS. ""

2) The call is complete via classfunction.prototype. [Functionname] = function (var1, var2 ...) {// TODO}.

These two ways are consistent from the goal. According to my personal point of view, the difference is that the implementation method is created by this.propertyName, and JScript automatically creates the entrance of Property or Method, but from the program A perspective, it is more flexible to use Prototype's keyword. In addition, JavaScript can also declare the method of nested declaration in our C , as follows

Public class classname: parentclass {

Public DataType functionName () {

}

Public class classname {

Public DataType functionName () {

}

}

}

In JavaScript, of course, there is no keyword such as a class, so it is a bit dramatic, but it is still a very clever implementation.

Function classname () {

// Property Implement

THIS.USERNAME = "blue";

// Method Implement

this.add = new function () {

}

// Sub Class IMPLEMENT

Function SubclassName () {

this.propertyName = "hi"

}

// Sub Class Method IMPLEMENT

Subclassname.prototype.change = function {

}

}

// main class method import

ClassName.Prototype.delete = function () {

}

As the code is roughly demonstrated in the implementation of the attributes and methods in the JavaScript class, there is a more confusion, and the entire class is public. There is no keyword private, which can control some methods to hide, then write code implementation In the specification, I think some of the foreign programmers are different from _functionName, which is different from the method of functionality, but actually calls during the calling process.

The property and method are realized, and the rest is Event's implementation. I have found a lot of information, including the entire MSDN for JScript, did not see a good model for incident, and later refer to some sites to write HTA (HTML Component, I have time I will write some related articles), and the method of event-driven can be roughly achieved by means of comparing the twisted (I personally think). The rough idea is this:

1). Define all events into properties, as long as the simple declaration can

2). Determine if the event attribute is a function in the code that triggered the event, if it is a function, execute the function code, if it is a string, then perform a string function, execute it via EVAL (STR).

3). Register the event function in an instance of the class.

In order to briefly explain the above ideas, use a simple example of Timer to express the above mentioned content. If it is just for simple implementation of Timer function, the setInterval function in JavaScript can meet all requirements, the following code is only used Description Timer's working principle.

// Class for TimerFunction Timer (IINTERVAL) {// if not set the Timer Interval, Then Defalut set to 500ms this.interval = interval || 500; this._handleinterval; this.timerevent = null function start () {ix (this .Interval! = 0) {this._handleinterval = setInterval ("TimerCallback ()", this.interval);}} function start () {clearinterval (this._handleinterval);} function timerCallback () {i (typeof this.timerevent == "function") {this.timerevent ();} else if (this.timerevent! = null && this.timerevent.Length> 0) {EVAL (THIS.TIMEREVENT);}}} // Code for instancevar t = New Timer (3); // ------------------------------------ //

//1.t.timerevent=function() ketto}

//2.t.timerevent="alert (/"hello/ ")";

// 3.

T.TimeRevent = TTIMERCALL;

// ---------------------------------- // T.Start (); t.stop ();

Function TTIMERCALL () {

}

The actual work code is implemented above the TimerCallback (), the event triggered as the property, in the application instance, the code provides three ways to call the event, but in the callback of the event, I haven't thought about how to take parameters. Only the properties of their respective implementations can be implemented in the respective implementations.

2. inherit.

Just use a large number of words to introduce various implementations of JavaScript, which is logically completed a package Class implementation. In a sense, the Class's implementation is the most used part of the true scripting program. However, if you just want to complete the above function, use VBScript to write more clearer, after all, VBScript provides a Class keyword, and provides both keywords of public and private, which can be clearly separated by public and private objects, as for events Implementation, can also use the idea of ​​JavaScript implementation, but to use the getRef function for the reference to the function, the specific usage can be referred to Scripting Reference, and the MSDN also has a detailed introduction, and JavaScript is powerful as it is to say, though Specific things may not have much.

As mentioned above, we have completed a basic class implementation Timer. Now you have to re-write this class. We simply just want to join a method in this class, provide the current system time, the name of the method is getSystemDate. Obviously, if all rewritten, then I lost the meaning of I said here. Let's take a look at the implementation.

Function newTimer (IINTERVAL) {

// Call Super Super SUPER

THIS.BASE = Timer;

THIS.BASE (IINTERVAL);

NEWTIMER.PROTYPE = New Timer;

NEWTIMER.PROTYPE.GETSYSTEMDATE = Function () {

VAR DT = New Date ();

Return dt.getYear () "-" DT.GETMONTH () "-" Dt.getDay ();

}

The above code implements the NewTIMER class. From Timer inheritance, JavaScript does not use ":" or Java's public keyword, just through the method such as new baseClassName.Prototype = New BaseClass, and newtimer implements getSystemDate method, in In the initialization function of NewTimer, I used this.base = Timer, which is to reference the parent class's implementation, but in the call to the other implementation functions of the parent class, until now I didn't find a certain method, whether I passed this.base. START () is called or other, if anyone is more clear, trouble tells me, on the site of Netscape, I find that there is a special "__proto__" property seems to be directly referenced for the parent class, but I didn't try to have some of the MSDN, and support for __proto__.

3. Overload

Perhaps this is a more complicated place in OOP program. It is a bit helpless in JavaScript, which is done through prototype, but because I don't know how to call the parent class's implementation function, then only in overload Rewind all the implementation, and also instantiate a parent class in the implementation, and then returns to the required thing by calling it.

All objects in JavaScript are inherited from Object. Object provides a method for TOString (), that is, if the process of calling Alert (Objinstance) is actually called Alert (Objinstance.Tostring ()) If there is no writing implementation, Object default toString () is "Object Object", in many places, you need to overload this function, such as Timer, if we want VAR INS = New Timer (5); Alert (INS The call to the value of the interval is 5, then you need to rewrite the toString () method.

Timer.Prototype.toString = function () {return this.interval};

After the above code is implemented, the Alert (INS) is 5.

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

New Post(0)