JavaScript: Class Expression

By Xah Lee. Date: .

Class can be created by expression form:

Class expression returns a value. Can be called inline.

// class is a expression. it returns a value. can be called inline
console.log(
  new (class Bb {
    constructor(x) {
      this.p = x;
    }
  })(3),
);
// Bb { p: 3 }

The name in x = class name {}; is optional. When given, it allows you to refer to itself inside the class body, but the name is not exposed outsite of it.

// the name of a named class expression is only available inside the class body

const Dd = class Cc {
  constructor(x) {
    this.p = x;
  }
};

new Cc(3);
// error: Uncaught ReferenceError: Cc is not defined

JavaScript Constructor/Class

BUY
ΣJS
JavaScript in Depth

JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Property

Object and Inheritance

Array

Function

Constructor/Class

Iterable 🌟

Misc