Skip to content

Instantly share code, notes, and snippets.

@nitin42
Last active February 26, 2018 18:36
Show Gist options
  • Save nitin42/0a551d5ab00743d4dab04f8d53d6e5f1 to your computer and use it in GitHub Desktop.
Save nitin42/0a551d5ab00743d4dab04f8d53d6e5f1 to your computer and use it in GitHub Desktop.
const CarVisitor = {
accelerate: function(visitor) {
console.log(`Yeah! I am driving a ${visitor.name}.`)
},
brake: function(visitor) {
console.log(`Brakes failed in ${visitor.name}.`)
}
}
class Car {
operate(carVisitor) {}
}
class Sedan extends Car {
constructor(name) {
super()
this.name = name
}
operate(visitor=CarVisitor) {
visitor.accelerate(this)
}
}
class Hatchback extends Car {
constructor(name) {
super()
this.name = name
}
operate(visitor=CarVisitor) {
visitor.brake(this)
}
}
const sedan = new Sedan('Sedan')
sedan.operate() // Yeah, I am driving a Sedan.
const hatchback = new Hatchback('Hatchback')
hatchback.operate() // Brakes failed in Hatchback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment