We assume that the reader of this book has at least a rudimentary knowledge of the practice of JavaScript. If you want to provide a comprehensive tutorial on JavaScript, this in itself requires a complete book to make it clear, so here we do not intend to introduce the language in detail. Instead, this section simply discusses some of the potentially obscure advanced features of JavaScript and explains how to use them in conjunction with your AJAX development.
Let's talk briefly about the history of JavaScript so you can see what it was like and how it developed today. Netscape's Brendan Eich developed JavaScript in 1995. His task was to develop a way to make it easier for the non-professional web designers who create and maintain Web sites to use Java applets. Eich that the right choice is to develop a weakly typed language that does not require a compiler.
Originally, Eich developed a language that had many names, but later, thanks to Java's success in the marketplace, it was finally named JavaScript by the Dongfeng. JavaScript quickly became the most popular scripting language on the web, thanks to its low threshold and the ability to copy JavaScript scripts from one page to another. Based on earlier revisions of JavaScript and navigator DOM, the DOM level 0 standard is generated, which defines form elements and images as child elements of a DOM element.
Microsoft is catching up, creating its own scripting language VBScript. VBScript is functionally similar to JavaScript, but uses class Visual Basic syntax and can only be used for IE. Microsoft also provides a single implementation JScript for JavaScript (now standardized by ECMA and called ECMAScript). Although the syntax of different JavaScript is almost the same, the DOM implementations on different browsers are so disparate that it is almost impossible to create cross-browser scripts. Scripts that are obtained using the "lowest common denominator" method usually do only the simplest tasks.
By the year 1998, Netscape opened up its browser's source code, decided to start rewriting browsers and focused on following the consortium's standards. At that time, IE 5 was the best implementation of the DOM and ECMAScript of the consortium. The open source version of Netscape was launched in 2002 in the name of Mozilla. As a result, the browser landscape has become a trend: more and more browsers are trying to follow web standards maintained by the consortium and ECMA. Today, modern browsers such as Firefox, Mozilla, Opera, Konqueror, and Safari are strictly compliant with web standards, which greatly simplifies the task of writing Cross-browser HTML and JavaScript. IE 6 is not much different from IE 5 in 1998, and it strictly prohibits the most substandard behavior.
5.7.1 to create object-oriented JavaScript via the prototype attribute
JavaScript supports inheritance through a linkage mechanism, rather than through a class-based inheritance model that is supported by fully object-oriented languages such as Java. Each JavaScript object has a built-in attribute named prototype. The prototype property holds a reference to another JavaScript object that acts as the parent object of the current object.
When a function or property of an object is referenced by a dot notation, the object's prototype property is used when the function or property is not on the object. When this occurs, the object prototype property is checked to see if there are any requested properties or functions. If the object referenced by the prototype property does not also have the required function or property, the prototype property of the object (the object referenced by the prototype property) is further examined, followed by the chain up, until the requested function or property is found, or the end of the chain is reached. Returns undefined if it has not been found at the end of the chain. In this sense, this kind of inheritance structure should be a "has a" relationship, not "is a" relationship.
If you are accustomed to a class based inheritance mechanism, it may take some time to familiarize yourself with this prototype mechanism. The prototype mechanism is dynamic and can be configured at run time as needed without recompiling. You can add attributes and functions to objects only when you need them, and you can dynamically merge individual functions together to create dynamic, omnipotent objects. The highly dynamic nature of the prototype mechanism is mixed because it is not easy to learn and apply, but when applied correctly, the mechanism is powerful and robust.
This dynamic is similar to the polymorphic concept in a class-based inheritance mechanism. Two objects can have the same properties and functions, but function methods (implementations) can be completely different, and properties can support completely different data types. This polymorphism allows JavaScript objects to be handled in a uniform manner by other scripts and functions.
Figure 5-15 shows the actual prototype inheritance mechanism. This script defines 3 types of objects: Vehicle, sports-
Car and Cementtruck. Vehicle is the base class, and the other two classes inherit from this. The vehicle defines two properties: Wheelcount and Curbweightinpounds, respectively, representing the number of wheels and the total weight of the vehicle. JavaScript does not support the concept of abstract classes (abstract classes cannot be instantiated and can only be extended by other classes), so for vehicle base classes, Wheelcount defaults to 4,curbweightinpounds defaults to 3 000.
Figure 5-15 The relationship between Vehicle, sportscar, and Cementtruck objects