Comment on "Object-Oriented JavaScript programming"

Source: Internet
Author: User

Object-oriented JavaScript programming

I have not read Netscape documentation, nor have I read ecmascript (ECMA-262) specifications, but msdn cannot really understand JavaScript.
Let me give you some guidance.

> Object-oriented JavaScript programming

Javascript can be considered as an object language, but it is different from C ++ and Java. The main difference is:
1. JS is an object-based language, rather than a strict object-oriented language.
2. js objects are prototype-based. What is prototype? Let's take a look at the prototype mode in design patterns.
3. js functions are first-class citizens. js does not distinguish class from object.

> JavaScript should not be unfamiliar to people who have used web programs. It was initially used for simple form verification and basically played with some technical skills. IE 4.0 introduced DHTML and proposed its own scripting language JScript to combat Netscape Javascript. In addition to complying with the EMAC standard, it also added many extensions, the following mentioned OOP programming is one of them. For the sake of life and concept, all the Javascript I mentioned below are jscripts implemented above Microsoft Internet Explorer 4.0. For Netscape, I have not done too many programs, so I can see some differences.

Many people only play with JS skills, which is actually a half bottle of water. Over-selling JavaScript code in browsers, but not having to write NoScript labels, is a typical manifestation of not understanding the web. There is nothing to say about the DHTML-vendor confrontation. Let's look at the DOM standard. By the way, Netscape has JS guides and references. It is recommended that people who really want to go deep into JS should go through them at least once, on msdn, although there are more than one version of the JS language, it is never as clear as the company that creates the language. Or I suggest you read Ole's Js authoritative guide. I have turned it over and it is one of the best JS books in history.

> Javascript is not an object-oriented language, but not a development platform. However, JavaScript provides a very powerful prototype-Based Object-Oriented calling function, you can use them wherever you need them. Therefore, how to use objects? This article tries to parse the working model from the principle of JavaScript object-oriented implementation. After learning about these models, you can write some implementation code in your script library and then call it elsewhere.

The author is right. js itself is a scripting language and relies on the host language (according to ECMA specifications ), therefore, many people are wrong about how JavaScript works, because they are about DHTML or Dom. In short, they are about the browser environment, not the characteristics of Js. Ms DHTML can be used for JS and vbs. Dom is language independent. JS itself is a pure language. Although it is mainly designed for the browser environment, it does not affect its role in other environments. For example, it can be used as a server script, such as ASP (although most vbs are used), or a JSP script language (note that JS is not JSP, JSP usually uses Java as the script language, but you can also use other scripting languages ). The "prototype-Based Object-Oriented calling function" statement is problematic. Prototype is not a function call, but a language mechanism. Moreover, the formulation of "Object-Oriented" is not completely accurate. In terms of usage, object-oriented usually refers to C ++, Java, smalltalk, etc. JS is only called "Object-based". Of course, I think this is not very important.

> The syntax of JavaScript is very similar to that of C ++, but the keyword class is not used in class implementation, during implementation inheritance, the so-called keywords such as public or implement are not used to mark the implementation of the class. In this case, someone may ask how to write the Javascript class and how to implement inheritance. I was puzzled at the beginning. After reading msdn, I realized that prototype was used for implementation, including inheritance and overloading. This keyword can also be used for implementation.

JS syntax is C (Java, C # is also ). Not only is there no keyword class, but strictly speaking, JS does not have a class (of course there is no inheritance ). But in general, we can think that JS has something similar to "class. C ++ is not a "complete" object language, because it lacks "class objects". (However, this is a different design of the language and may not be a disadvantage ). As for IMP, it is an interface. Many languages use multiple inheritance, virtual classes, and abstract methods to simulate interfaces without explicit interfaces, such as C ++. Prototype is not just a keyword, but one of the characteristics of Js. How to inherit, although you can explore it from msdn, it is only a half of the effort. I suggest you directly read the Netscape guide or Mozilla.

> JavaScript Functions are very strange. Each function implements optional by default, that is, parameters can be optional. function a (var1, var2, var3 ), in the call process, a (), A (value1), A (value1, value2) and so on are all correct, at least even if the Compilation part can be completely passed, it is only related to the Implementation logic of the function.

There is nothing strange about this, but there are many features of JS (dynamic type, no function with the same name ......) . In addition, you can use arguments to operate parameters like an array within the function.

> The following describes how to implement, inherit, and overload classes in JavaScript.
> 1. Implementation
> JS class implementation is directly implemented through functions. Each function can be regarded as a class directly. The following code:

It should be said that JS has no class, but everything in JS is an object, including a function. Therefore, classes can be simulated by creating a constructor (note that constructor constructs objects rather than constructor classes.

> The attributes of a class can be implemented in two ways.
> 2) use classfunction. Prototype. [functionname] = function (var1, var2. ..) {// todo} to complete the call.

Correct the words first. Here we do not call but assign values.

> The two methods are consistent in terms of goals. From my personal point of view, the difference is that the implementation method is implemented through this. propertyname is created. JScript automatically creates the entry to the property or method. However, from the program perspective, it is more flexible to use the prototype keyword.

The effects of the two methods are basically the same, but there are still important differences, that is, the former is a value assignment in the constructor, and the latter is an external value assignment through the prototype object. If there is a complicated "class hierarchy", you will find that the two are different, and the order is wrong. The latter is more flexible, but you need to know that JS is a completely Dynamic Language, you can even change the constructor dynamically.

> In addition, JavaScript can be declared with the nested declaration method in C ++. The Implementation Method of C ++ is as follows:
> In Javascript, of course, there is no such keyword as class, so the implementation is a little dramatic, but it is still a very clever implementation.
> Function classname (){
> // Property implement
> This. Username = "blue ";
> // Method Implement
> This. Add = new function (){
^
The author has a pen error. This new is redundant.
 
>}
> // Sub class implement
> Function subclassname (){
> This. propertyname = "hi"
>}
>

By the way, the author excessively uses the object. Prototype. Prop = value method, which is actually not good. This destroys the structure of the "class. In addition to directly assigning prototype objects to simulate inheritance, it should not be used unless necessary.

> The code above roughly demonstrates the implementation of attributes and methods in the Javascript class. In addition, it is a bit confusing that the entire class is public and there is no such keyword as private to control whether some methods are hidden, in our coding and implementation specifications, some programmers abroad use the _ functionname method to differentiate the function life, but they can still be called during the call process.

Public is only used to increase the reliability of the language. It is not necessary for the scripting language.

> Attributes and methods are implemented, and the rest is the implementation of event. I have searched for a lot of information, including the entire msdn reference for JScript, I didn't see a good model about event implementation. Later I referred to some sites to compile the implementation of HTA (HTML component, I will write some related articles if I have time, with the help of the relatively distorted (I personally think) method, we can roughly implement the event-driven function. The general idea is as follows:
> 1) define all events as attributes. You can simply declare them.
> 2 ). determine whether the event attribute is a function in the code that needs to trigger the event. If it is a function, directly execute the function code. If it is a string, execute the string function and run it through eval (STR.
> 3) register the event function in the instance of the class.

If you do not comment on this part, go to the DOM 2 event model.

> 2. Inheritance.
> I just used a large article to introduce how to implement various JavaScript implementations, that is, to logically complete the implementation of an encapsulated class. In a sense, the implementation of class is the most commonly used part in real Script Programming. However, if you only need to complete the above functions, it is clearer to use Vbscript to write. After all, VBScript provides the class keyword, at the same time, the public and private keywords are provided to clearly separate public and private objects. For event implementation, the idea similar to javascript can also be used, it is only necessary to use the getref function for function reference. For specific usage, refer to the scripting reference, which is also described in detail in msdn. The powerful JavaScript lies in the following, although there may not be many specific things.

For vbs, I only want to say that vbs is the junk of the M $ card.

> The above code implements the newtimer class, inherited from timer. JavaScript does not use the keyword similar to ":" or Java's public, but only uses newclassname. prototype = new baseclass and newtimer implements the getsystemdate method. In the newtimer initialization function, I used this. base = timer is used to reference the implementation of the parent class. However, when calling other implementation functions of the parent class, I have not found a definite method to determine whether to pass this. base. start () is called like other. If anyone knows clearly, please let me know. In addition, on the site of Netscape, I found that a special "_ PROTO _" attribute seems to be a direct reference to the parent class, but I have not tried it, no support for _ PROTO _ is displayed in msdn.

The reason why it is not inherited like other languages is that it is not a "real" inheritance, because JS does not have a "real" class at all. Prototype is a reference to a prototype object. Calling the "parent class" method is actually very simple. You can directly call the constructor called the "parent class. As for why this. base = parent is used, it is easy to understand one thing. Parent is not actually a class, but a constructor. In the "subclass" constructor, no prototype exists at the time (prototype is an external dynamic value assignment, not static information ). Therefore, you must manually obtain the "parent class" constructor and run it.

Someone may ask, why prototype? Isn't it enough to write everything in the constructor? Hey, think about the author's "second method ". Prototype has a feature that adds an attribute (including methods) to the prototype. This attribute is automatically added to all objects that inherit the prototype. Therefore, if you want to use this feature, you still need a prototype. At least you can use the instanceof operator to determine the variable type.

_ PROTO _ can be detected and traced back to form a prototype chain, which is useful in complicated "class structures". You can use it to write your own instanceof. However, this is not the requirement of ECMA specifications. It should also be used only within the language (do you really need to dynamically change the parent class in the subclass ?).

> 3. Heavy Load
> Maybe this is a complicated part of OOP programming. It is helpless in Javascript implementation, that is, it is done through prototype, however, because I don't know how to call the implementation functions of the parent class, I can only rewrite all the implementations in the overload, and instantiate a parent class in the implementation, then, you can call it to return what you need.

This is just a matter of sorrow.
It is no big deal to instantiate a parent object. Do not stick to the "Orthodox" oo concept. Think about it. During prototype inheritance, do you actually not instantiate a parent class object and assign it to prototype? It's just that this is not a really needed instance, but a "model", so you may not need to pass it to any parameters of the constructor. Calling the "parent class" method does not necessarily require instantiating a parent class object. Recall the process of calling the parent class constructor. After the call, you have obtained all the attributes and Methods initialized by the parent class constructor. Whatever you want.

> All objects in JavaScript are inherited from objects. The object provides the tostring () method. That is to say, if alert (objinstance) is called, alert (objinstance) is called. if the tostring () method is not implemented, the default tostring () of the object is "Object object". In many places, you need to reload this function, such as timer, if we want var ins = new timer (5); alert (INS) to call to obtain the value of interval 5, we need to rewrite the tostring () method.

In addition to tostring, you can also rewrite valueof to return a native value to your object.

> After a lot of nonsense, I finally gave a rough idea. In fact, language is just an implementation tool. What is important is the design idea. You may consider it, develop an opensource project in Biti. If a javascript-based model is used to build a development platform library, I hope someone can participate. Using JavaScript to create a series of web UI-based controls, I am also based on the above ideas in the development process. In addition, the class source program similar to the Hotmail button I wrote last year is attached, and image preload is not used yet. I hope someone can help me modify it. If you need a version that can be run, send me an email: liuruhong@263.net. In addition, I will write JavaScript-based component programming and multimedia programming, and then the XML aspect. I hope everyone can make common progress.

Language is a tool and has different application goals. Design Ideas can certainly be shared, especially the object language. You can use JavaScript to quickly develop a prototype (the prototype in the general sense is not the previous technical term), and then use Java and C ++ as the final product. As for webui writing with JS, I need to pour a little bit of cold water, so don't worry too much. Webui is mainly not about JS, but about browsers and other technical standards. For example, Mozilla UI is written in XUL. In addition, many people think that XBL and HTC (both using js to encapsulate Web controls) are not very good directions (although I once liked these two technologies), because there is no good Orthogonal Decomposition, obfuscated style and Js. The future direction depends on the W3C Standardization Organization. Therefore, JavaScript does not have to write complex class structures (script language is easy to use after all), so you need to spend more energy on Dom and compatibility.

Finally, Netscape is still developing JS 2.0 (for several years), and ECMA is also working as Edition 4 (M $ is not clear about its trend, in addition, its JScript has not yet fully implemented Edition 3). This version seems to have added keywords such as class and public, which may be greatly changed. Interested people can see: http://www.mozilla.org/js/language/js20/index.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.