JS: 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 {body}; is optional. When given, it allows you to refer to itself inside the class body, but the name is not exposed outside 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