How to declare class attributes easily?

class Autobot {
  name = 'Optimus Prime';
  fraction = 'Autobots';
}

const transformer = new Autobot();

// output: I am Optimus Prime and leader of the Autobots!
console.log(`I am ${transformer.name} and leader of the ${transformer.fraction}!`);
Copy

August 1st, 2022

# Class attribute declaration

Add to bookmarks

In object-oriented programming, class attributes are often declared in the constructors. In JS, no constructor is needed.

HomeBookmarks