JavaScript s Object-Oriented Reality


Today, object-oriented programming (or OOP) is commonly accepted as a good way to structure programs, but rarely is full-blown OOP style used in JavaScript. You might wonder why this is. The language itself does support the principles of object-oriented programming, which have been demonstrated in the examples of this chapter and are summarized here:

  • Abstraction An object should characterize a certain abstract idea or task. The object should present an interface to the programmer that provides the features or services one might expect of an object of that type.

  • Encapsulation An object should maintain internally the state necessary to characterize its behavior. This data is usually hidden from other objects and accessed through the public interface the object provides.

  • Inheritance The language should provide the means for specialized objects to be created from more general objects. For example, a general Shape object should lend itself to the creation of more specific objects, like Squares , Triangles , or Circles . These specific objects should inherit capabilities from their ancestors .

  • Polymorphism Different objects should be able to respond in different ways to the same action. For example, Number objects might respond to the operation of addition in the arithmetic sense, while String objects might interpret addition as concatenation. Additionally, objects should be allowed to polymorph ( change shape ) depending upon context.

JavaScript supports all of these principles; they are clearly present in the language itself. However, in practice they are largely ignored by most programmers writing their own scripts. This lack of OOP programming style in JavaScript is due to the tasks it tends to be used for and the ease of employing other approaches to accomplish those tasks. The value of using many of its structures is questionable because the size and complexity of most scripts are not sufficient to warrant the use of an OOP approach. In fact, the success of the language for many of its users is that it doesn t take a great deal of effort or lines of code to accomplish useful tasks within Web sites. Most JavaScript programmers use those features they find convenient and leave the major OOP features to those writing full-fledged Web applications.




JavaScript 2.0
JavaScript: The Complete Reference, Second Edition
ISBN: 0072253576
EAN: 2147483647
Year: 2004
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net