Skip to main content

Command Palette

Search for a command to run...

[JavaScript] Object-Oriented Programming (OOP) with JavaScript : ES6 Classes

Published
•View as Markdown
[JavaScript] Object-Oriented Programming (OOP) with JavaScript : ES6 Classes
// class expression
// const PersonCl = class{

// }

// class declaration
class PersonCl {
  constructor(firstName, birthYear) {
    this.firstName = firstName;
    this.birthYear = birthYear;
  }
  // Methods will be added to .prototype property
  calcAge() {
    console.log(2037 - this.birthYear);
  }

  greet() {
    console.log(`Hey, ${this.firstName}`);
  }
}

const jessica = new PersonCl('Jessica', 1996);
console.log(jessica);
jessica.calcAge();

console.log(jessica.__proto__ === PersonCl.prototype);

// PersonCl.prototype.greet = function() {
//   console.log(`Hey, ${this.firstName}`);
// }
jessica.greet();

// 1. Classes are NOT hoisted.
// 2. Classes are first-classed citizen.
// 3. Classes are executed in strict mode.

ES6 classes.PNG

JavaScript

Part 1 of 50

More from this blog

Jayden's Blog

166 posts