In this column we conclude our short series about Object-Oriented Programming (00P) with JavaScript. In Part I of this series, we covered the fundamentals. In this column, we focus more on encapsulation, especially on methods. As we discussed in the first part of this series, JavaScript‘s support of object-oriented programming is quite impressive. Even though there are no formal classes or instances, there are objects, prototypes, and methods that mimic them. In fact, it is the methods that make the language so vibrant and powerful. You use methods to mimic class inheritance between objects. We‘ll show you how to define functions in JavaScript, and how to define methods with these functions. We‘ll show you where method definition does not match the rules for functions. We‘ll explain the exposure of private data elements, and how to protect them. In this column, we also cover contexts, scopes, and context switching. Understanding contexts and scopes will help you in writing as well as debugging complex scripts. The examples in this column are for JavaScript 1.1 and above, for both Internet Explorer and Netscape, unless noted otherwise. In this column, you will learn: Constructing ObjectsAn object constructor is written as a regular function. Here is the
When you create an object, it automatically inherits all
The
Alert the object. You should get the string you passed to the constructor, |
|