Book Image

Learning Object-Oriented Programming

By : Gaston C. Hillar
Book Image

Learning Object-Oriented Programming

By: Gaston C. Hillar

Overview of this book

Table of Contents (16 chapters)
Learning Object-Oriented Programming
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Working with duck typing in JavaScript


We will create an Animal constructor function and customize its prototype to generalize all the requirements for animals. We will create two constructor functions: Dog and Frog that will use Animal as its prototype. Then, we will create a Party constructor function that will be able to work with instances of any object that includes Animal in its prototype chain through duck typing. We will work with the party of dogs and the party of frogs.

Then, we will create a HorseDeeJay constructor function and generate a new version of the Party constructor function that will work with any object that includes Animal in its prototype and any object that provides the properties and methods declared in the HorseDeeJay prototype through duck typing. We will work with the party of dogs with a DJ.

Declaring a constructor function that defines the generic behavior

Now, we will declare the Animal constructor function. Then, we will add properties and methods to its prototype...