Skip to content

Instantly share code, notes, and snippets.

@jamiebullock
Last active September 4, 2020 07:58
Show Gist options
  • Save jamiebullock/79c4de2d87726675984c01e92613ce78 to your computer and use it in GitHub Desktop.
Save jamiebullock/79c4de2d87726675984c01e92613ce78 to your computer and use it in GitHub Desktop.
Dynamic dispatch
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