Last active
February 26, 2018 18:36
-
-
Save nitin42/0a551d5ab00743d4dab04f8d53d6e5f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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