Last active
September 4, 2020 07:58
-
-
Save jamiebullock/79c4de2d87726675984c01e92613ce78 to your computer and use it in GitHub Desktop.
Dynamic dispatch
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 handleShape = (shape) => { console.log(shape.area()); } | |
class Shape | |
{ | |
constructor(width = 2) | |
{ | |
this.width = width; | |
} | |
area() | |
{ | |
return 0; | |
} | |
} | |
class Square extends Shape | |
{ | |
area() | |
{ | |
return this.width ** 2; | |
} | |
} | |
class Circle extends Shape | |
{ | |
area() | |
{ | |
return Math.PI * (this.width / 2) ** 2; | |
} | |
} | |
const shape = new Square(); | |
handleShape(shape); // logs 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment