Skip to content

Instantly share code, notes, and snippets.

@ardeshireshghi
Created August 24, 2021 09:54
Show Gist options
  • Save ardeshireshghi/583deeef49b70d45fdb9fea0044c1d84 to your computer and use it in GitHub Desktop.
Save ardeshireshghi/583deeef49b70d45fdb9fea0044c1d84 to your computer and use it in GitHub Desktop.
// Old school classic inheritence
function Shape(name) {
this.name = name;
}
Shape.prototype.getName = function() {
return this.name;
}
function Square(width, height) {
Shape.call(this);
this.width = width
this.height = height
}
Square.prototype = new Shape('square')
// Mid-school
function Shape(name) {
this.name = name;
}
Shape.prototype.getName = function() {
return this.name;
}
function Square(width, height) {
Shape.call(this, 'square');
this.width = width
this.height = height
}
Square.prototype = Object.create(Shape.prototype)
// Late-school
class Shape {
constructor(name) {
this.name = name
}
getName() {
return this.name
}
}
class Square extends Shape {
constructor(width, height) {
super('square')
this.width = width
this.height = height
}
}
// State of the art - TS
class Shape {
constructor(public name: string) {}
getName(): string {
return this.name
}
}
class Square extends Shape {
constructor(public width: number, public height: number) {
super('square')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment