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 like Immediately Invoked Function Expression.
// class is a expression. it returns a value. can be called inline console.log( new class CC { constructor(x) { this.pp = x; } }(3), ); // CC { pp: 3 }
Name optional
when assigned to a variable
varname = class class_syntax_name {body};
The class_syntax_name is optional. The varname is the constructor name.
When class_syntax_name is given, it is available inside the class body. and class_syntax_name is the tttttttt xxxxxxxx
both varname class_syntax_name are available inside to class body.
// the name of a named class expression is only available inside the class body const Burger = class Sammy { constructor(x) { this.meat = x; } }; try { new Sammy(3); } catch (xerror) { console.log(xerror); } // ReferenceError: Sammy is not defined const myburger = new Burger("beef"); console.log(myburger); // Sammy { meat: "beef" }
/* in varname = class class_syntax_name {body}; both class_syntax_name and varname are available in class body. but both refers to the same constructor. */ const Burger = class Sammy { constructor() { console.log(Burger); console.log(Sammy); } }; const myburger = new Burger(); console.log(myburger); // prints // [class Sammy] // [class Sammy] // returns // Sammy {}