In my last article, I discussed how to create classical class objects in JavaScript. In this article I’m going to continue the discussion by looking at some advanced concepts — classes as first-class objects, computed member names, and static member variables.

Classes are First-Class Objects

A first-class object is an object that can be a function parameter, the return value of a function, and assigned to a variable in the same way that primitive data, such as numbers and strings, can do those things.

Let’s start with class objects being assigned to variables. The following example demonstrates how an anonymous class expression can be assigned to a variable, creating a singleton class. Here is the code:

let MyMath = new class {
  get PI() {
    return 3.14159;
  }
}
let radius = 5;
let area = MyMath.PI * (radius * radius);
print(`The area of the circle with radius 5 is ${area}.`);

#learn-to-program #javascript #javascript-object #machine-learning

Learning JavaScript: Introduction to Classes
1.30 GEEK