It doesn't matter if it's a duck as long as it behaves (properties and methods match) like a duck.
class Duck {
constructor(name) {
this.name = name
}
takeOff() {
console.log(`${this.name} is taking off`)
}
fly() {
console.log(`${this.name} is flying`)
}
}
Instances:
> const myDuck = new Duck('Larry')
> myDuck.name
'Larry'
> myDuck.takeOff()
Larry is taking off
> myDuck.fly()
Larry is flying
> const myPlane = new Duck('Roger')
> myPlane2.name
'Roger'
> myPlane2.takeOff()
Roger is taking off
undefined
> myPlane2.fly()
Roger is flying
Issues:
- Inaccurate. Can lead to false positives.
- The object's methods and properties have to be over arbitraty. How do you get agreement from other developers?