Chapter 7: Working with Objects


Overview

Objects, objects everywhere: But what are they and how do they work? The move to object-oriented programming—called “OO” or “OOP” when you want to use an acronym—has been one of the most important trends in the programming community over the past 10 or 20 years.

This chapter addresses a number of interrelated topics, each of which is important in its own right.

To start with, what is object-oriented programming? Why is it important? What is an object? How do objects and classes relate?

The focus of this book is about learning how to program and the craft of programming. It’s definitely not about the ins and outs of JavaScript. JavaScript is used as a teaching language—because it’s good to be able to see the consequences of one’s work in action.

That said, it’s still worth spending some time understanding how objects work in JavaScript. What are the objects built into JavaScript? How can you effectively use JavaScript objects? (I’ve called the section of this chapter that explicitly addresses these issues “ JavaScript OO Lite.”)

It turns out that JavaScript contains some OO features but isn’t fully OO in the ways that languages such as C# and Java are. (For one thing, there’s no explicit notion of a class, or object template, in JavaScript, as I explain in more detail later in this chapter.) However, it also turns out that one can program in a very OO fashion in a not-very-OO language—just as one can skip the OO and bang out straight procedural code in an OO language such as C# or Java. (By the way, I really don’t recommend the latter of these practices.)

We’ll take the leap with JavaScript and use its features to understand the essential points of object-oriented programming in general and specifically how to implement OO in JavaScript. How do you create and modify your own objects in JavaScript? How are objects best used in your programs?

JavaScript may not have classes, but, as I’ll show you, it does have prototypes, which are used to manage object inheritance. How does a prototype differ from a class, and why should you care? How can you work with prototypes in JavaScript to get a good feeling for the power of classes and inheritance generally?

By the end of this chapter you’ll be speaking, squeaking, dreaming, and rolling in objects—and, most important, you’ll have learned to think like an object-oriented programmer.




Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

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