Skip to content

Instantly share code, notes, and snippets.

@onejohi
Created May 23, 2020 10:20
Show Gist options
  • Select an option

  • Save onejohi/f9720274641d99da4c335c78064066fb to your computer and use it in GitHub Desktop.

Select an option

Save onejohi/f9720274641d99da4c335c78064066fb to your computer and use it in GitHub Desktop.
// an absctract representation
class Person {
constructor(name,email) {
this.name = name
this.email = email
}
// things a person can do (functions/methods within a class)
sayName() return `My name is ${this.name}`;
emailAddress() return this.email;
}
const person = new Person('Tony','[email protected]')
person.sayName() // Tony
person.emailAddress() // [email protected]
// or a mathematical representation
class Rectangle {
constructor(height, width) {
this.height = height
this.width = width
}
// things you can do to a rectangle
getPerimeter() return this.height + this.height + this.width + this.width;
getArea() return this.height * this.width;
}
const rect1 = new Rectangle(30, 30);
console.log(rect1.getPerimeter()); // 120
console.log(rect1.getArea()); // 900
// more complex
const rectangles = [{h:30,w:30},{h:45,w:30},{h:30,w:366}]
let instancesOfRectangleClass = []
rectangles.forEach((rectangle) => instancesOfRectangleClass.push(new Rectangle(rectangle.h, rectangle.w)));
instancesOfRectangleClass.forEach(r => console.log(r.getArea()))
// result
// 900
// 1350
// 10980
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment