
PondDuck class that encapsulates properties (state) and methods (behavior) for each duck instance.
Each instance (
daffy, donald) maintains independent state — calling daffy.fly() does not change donald. The favoriteFood?: string uses TypeScript’s optional property syntax so the constructor can omit it.
Key takeaways:
- Use classes to encapsulate related state and behavior.
- Optional properties use the
?operator and can be omitted during construction. - Methods should manage state transitions (e.g.,
fly()/land()checkisFlyingbefore changing it). - Instances are independent; operations on one instance do not affect others.