Object-oriented JavaScript programming
The author did not have seen the Netscape document, nor did it read the ECMAScript (ECMA-262) specification, just watching MSDN can't really understand JavaScript. Let me guide it. :)
> Object-oriented JavaScript programming
Although JavaScript can think of an object language, it is different from C , Java, which is familiar with. The main difference is: 1. JS is an object-based language rather than a strict object-oriented language. 2. The object of JS is based on prototype. What is Prototype? You can take a look at the Prototype mode in Design Mode. 3. JS's Function is a class, JS does not distinguish between Class and Object.
> JavaScript For people who have done a web program should not be unfamiliar, the initial is used to do some simple FORM verification, basically in playing 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.
Many people just play JS skills, in fact, is a half bottle of water. Excessively sell JS in the browser, but don't know how to write the Noscript tag, this is a typical still not understanding the performance of the Web. DHTML This kind of manufacturer confrontation has nothing to say, everyone go to the DOM standard. Taking the brothers, Netscape has JS's Guide and Reference. It is recommended that people who really want to go deep into JS to see it. Although the explanation of the JS language on the MSDN is more than a version, but it is better to create a language. Clear. Or recommend everyone to see Olei's "JS Authoritative Guide", I turn over, one of the best JS books ever.
> JavaScript is not an object-oriented language, more impossible to have a development platform, but JavaScript provides a very powerful prototype-oriented object 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.
The author is right here, the JS itself is a scripting language, and it is dependent on Host's language (according to the ECMA specification), many people are wrong about how JS is wrong, because they say DHTML or DOM In short, it is a browser environment, not the characteristics of the JS language. MS's DHTML can be used in JS and VBS, and DOM is independent of language. JS itself is a simple language, although mainly in the browser environment as design goals, but does not affect his role in other environments. For example, it can be used as a server-side script, such as ASP (although most VBS), or JSP's script language (Note JS is not JSP, JSP usually uses Java as a scripting language, but can also be used in other scripting languages). "Prototype's object-oriented call function" has a problem. Prototype is not a call function, but a mechanism for language. And "object-oriented" is not completely accurate. From the terminology, object-oriented general refers to C , Java, Smalltalk, etc., JS is only called "object-based", of course, I don't think this is not very important. > JavaScript syntax and C are very close, but do not use keyword class in class implementation, and do not use traditional public or import or other so-called keywords to mark the implementation when the inheritance is used. 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.
The syntax of JS is C-all road (Java, C # is also). Not only there is no keyword class, strict, JS is not class (of course, there is no inheritance). But in a popular, it can be considered that there is something similar to "class" in JS. In the way, C is not a "complete" object language, because lack "class object", huh, huh, huh, huh, this is the different design of language, not necessarily a shortcomings). As for IMP, it is an interface. Many languages have no obvious interface and simulate multiple inheritance, virtual classes, abstract methods, such as C . Prototype is not just a keyword, but one of JS's qualities. How to inherit, although you can explore from MSDN, just a half, or advise you to see Netscape's Guide, or Mozilla.
> 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. is correct, at least even if the compilation section can be fully passed, as other, just compare the function's implementation logic.
There is nothing strange, just many of the characteristics of JS (dynamic type, no symbol function ...). Supplementary one, within the function, you can use the Arguments like an array to operate parameters.
> The following implementations, inherit, and overload detail in JS, inherit, inherit, and overloaded. > 1. Implementation> The implementation of the JS class is directly implemented by the function, each function can directly see the Class, as follows
It should be said that JS has no class, but in JS, everything is an object, including functions. Therefore, the class can be simulated by doing a constructor (note, the constructor is to construct the object rather than the construct class). > For the properties of the class, you can implement the ClassFunction.Prototype. [FunctionName] = Function (VAR1, VAR2 ...) {// TODO} in two ways.
Correct words first, not calling but assignments.
> These two ways are consistent from the goal, according to my personal point of view, the difference is just the implementation method, created by this.propertyName, JScript automatically creates the entrance of Property or Method, but from the program From the perspective, it is still more flexible to use Prototype's keyword.
These two method effects are basically consistent, but there is still a more important difference, that is, the former is assigned to the constructor, and the latter is assigned from the outside through the prototype object. If there is a more complex "class level", you will find the difference between the two, and the order problem :) The latter is more flexible, but you must know that JS is a completely dynamic language, you can even dynamically change the constructor, huh, .
> In addition, JavaScript can also declare the method of nested declaration in our C , the method of C implementation is as follows> Of course, there is no keyword such as Class, so it is a bit dramatic, but still a very Smart implementation. > Function classname () {> // Property Implement> this.username = "blue";> // Method Implement> this.add = new function () {^^^ Author a small pen error, this New is extra. >}> // Sub Class Implement> Function SubclassName () {> this.propertyName = "hi">}>
In the way, the author uses Object.Prototype.Prop = Value's method, which is not good. Because this destroys the structure of "class". In addition to directly assigning prototype objects to simulate inheritance, if it is not necessary, it should not be used.
> As above code, the implementation of the properties and methods in the JavaScript class is roughly demonstrated, and there is a little confusion, and the entire class is public. There is no keyword private, which can control some methods to hide, then write code in our write code. In the implementation of the specifications, I see some of the foreign programmers to use _functionName to distinguish between the functionality of the function, but actually call in the calling process.
Publici, just to increase the reliability of the language, is not necessary for scripting languages.
> Realize the properties and methods, the rest is Event's implementation, I find a lot of information, including the entire MSDN for JScript, did not see a good model for the event, and later referred to some sites HTA (HTML Component, I will write some related articles), and the method of event-driven can be roughly achieved by means of comparing (I personally think). The general idea is this:> 1). Define all the events into the property, as long as the simple declaration can be> 2). During the code that triggered the event, it is a function if it is a function, if it is a function, direct Perform a function code, if it is a string, then perform a string function, execute it via EVAL (STR). > 3). Register event functions in an instance of the class. This part does not comment, go to the event model of the DOM 2.
> 2. inherit. > Just use a large number of words to introduce various implementations of JavaScript, which is logically completed the implementation of a package Class, in a sense, Class's implementation is the most used part of the real script programming. However, if you just have to complete the above features, use VBScript to write more clearer, after all, VBScript provides the class keyword, and provides both keywords of public and private, which can be clearly separated by public and private objects, as for The implementation of the event can also use the idea of JavaScript implementation. It is only necessary to use the GetRef function for the reference to the function. The specific usage can be referred to Scripting Reference, and the MSDN has a detailed introduction, and JavaScript is powerful as it is to say, Although there may be few specific things.
I only said that VBS said: VBS is a m-card garbage.
> The above code implements the NewTIMER class. From Timer inheritance, JavaScript does not use ":" or Java's public as a keyword, just through new classname.prototype = new baseclass, and newtimer implements getSystemDate method, In the newtimer's initialization function, I used this.base = Timer, in order to reference the parent class's implementation, but in the call to the parent class other implementation function, I don't find a determined method, whether I have passed this.base .Start () is called or other, if anyone is clear, trouble tell me, on the site of Netscape, I check that there is a special "__proto__" property seems to be directly referenced for the parent class, However, I haven't tried specific, and I didn't see the support for __proto__ in MSDN.
The reason is not inherited like other languages, that is because this is not "real" inheritance, because JS does not have "real" class at all, huh, huh. The essence of Prototype is a reference to prototype objects. As for the "parent class" method, it is actually very simple. After calling the "parent class" constructor, it is directly called. As for why this.base = parent, it is very simple to understand that something is very simple. Parent is actually not class, but constructs a function. In the "subclass" constructor, there is no prototype presence (Prototype is an external dynamic assignment, not static information). So you must manually get the "parent class" constructor and run it. Maybe someone wants to ask, since subclasses directly run the parent class constructor, do you want to prototype? Write all things in the constructor not a knot? Hey, think about the author's "second method". Prototype has a property that adds an attribute (including method) to the prototype, and all objects that inherit this prototype automatically add this property. Therefore, if you want to use this feature, it still needs prototype. At least you can use the InstanceOf operator to determine the type of variable.
__proto__ can be detected and retracted, thereby forming a prototype chain, which is useful in a more complex "class structure", you can use it to write your own InstanceOf. However, this is not the ECMA specification, should also be used inside the language (do you really need to change the parent class in the subclass?).
> 3. Overload> Perhaps this is a more complex place in OOP programming. It is a bit helpless in JavaScript, which is done by prototype, but because I don't know how to call the parent class's implementation function, then overloaded Only the implementation can only be rewritten, and the other is instantiated in the implementation, and then it is necessary to return to what you need by calling it.
This helplessness is just the author worry and worry :) Instantiate a parent class object does not have a big deal, don't stick to "orthodox" OO concept. Think about it, when you inherit the prototype, do you actually instantiate a parent class object and assign a value to protoype? Just this is not an example of a truly need, but it is just a "mold", so maybe you can pass any parameters for constructor. The method of calling the "parent class" does not necessarily need to instantiate a parent class object. Recall the process of calling the parent class constructor. After calling, you have obtained all properties and methods initialized through the parent class constructor. To do anything else.
> All objects in JavaScript are inherited from Object. Object provides the method of TString (), that is, if the process of calling Alert (Objinstance), actually calls Alert (Objinstance.Tostring ()) Method, if there is no 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) Call is the value of INTERVAL 5, then you need to rewrite the toString () method.
In addition to toString, you can also override the value of valueof to return to your object to return a native type of value, huh, huh.
> The long art of tired said a pile of nonsense, finally said that the rough idea, in fact, the language is just a tool, it is important to design the idea, you can consider it, develop an OpenSource's Project in Biti, if it is based on JavaScript's model to establish development platform libraries, I hope someone can participate. Establish a series of Web UI-based controls through JavaScript, which is currently based on the above ideas during the development process. In addition, it attached to the Class source program like I wrote like a hotmail button last year. I haven't used image preload for the time being. I hope someone can help me modify it. If you need to run the version, send me email: liuruhong@263.net . In addition, I will write the component programming and multimedia programming section based on JavaScript, and then it is XML, I hope everyone will progress together. Languages are tools, there are different application objectives. Design ideas can of course work together, especially object language. You can quickly develop a prototype using JS (the prototype in general sense, not the previous technical term), then use Java, C to make a final product. As for writing WebUI with JS, I have to splash a little cold water, don't have to care too much. The problem of WebUI is mainly JS, but the browser itself and other technical standards. For example, Mozilla's UI is written in XUL language. Moreover, many people think that XBL and HTC (all to encapsulate web controls with JS), etc. (although I like these two technologies), because there is no good orthogonal decomposition, put the style, JS, etc. Confused. The future direction is also given by W3C's standardized organization. So JS doesn't have to write a very complex class structure (after all, the scripting language is to give people a relaxed), more energy flowers in Dom and compatibility.
Finally, Netscape is still developing JS 2.0 (for several years), ECMA also follows simultaneously with Edition 4 (M $ unclear the movement, and its JScript has not fully implemented Edition 3), this version seems to join. Keywords such as Class, Public, may have a large change. Interested people can go to see: http://www.mozilla.org/js/language/js20/index.html