This chapter focused on using duck typing instead of relying on type checking. We see how the dynamic nature of JavaScript and its type system do not allow us to process objects in a reliable way. The structure of an object can change dynamically and establishing the type of instance of an object that may be very difficult, for example, when multiple inheritance is applied. Therefore, we introduced the concept of a contract between objects and the support of interfaces provided by classical OOP languages.
Since JavaScript does not support interfaces, we explored the duck typing technique in order to describe and check the contract's compliance. Duck typing allows us to process objects focusing on how an object behaves instead of relying on what it is and what its type is.
Then, we elaborated an approach that allows us to emulate classical OOP interfaces and make some considerations about the relationship between duck typing and polymorphism.
In the next chapter, we will discuss different...