JS: Class Expression
Class can be created by expression form:
const x = class {body};
const x = class name {body};
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
- JS: this Binding
- JS: What is Constructor
- JS: Property Key "prototype"
- JS: Operator “new”
- JS: instanceof Operator
- JS: Property Key "constructor"
- JS: Difference Between typeof, instanceof, constructor property
- JS: Class
- JS: Class Syntax
- JS: Class Expression
- JS: typeof Class
- JS: Keyword “static” (static method)
- JS: Keyword “extends”
- JS: Keyword “super”
- JS: Define a Class Without Using Keyword class