What is ES6 class syntax in JavaScript?
Answer
ES6 classes are syntactic sugar over JavaScript's prototype-based inheritance — they provide a cleaner syntax but the underlying mechanism is still prototypes. Define: class Animal { constructor(name) { this.name = name; } speak() { return `\${this.name} makes a sound`; } static create(name) { return new Animal(name); } }. Inheritance: class Dog extends Animal { speak() { return `\${this.name} barks`; } }. super(name) calls the parent constructor; super.method() calls the parent method. Class fields (ES2022): class Counter { count = 0; #privateField = "secret"; }. Private fields (#) are truly private — inaccessible outside the class. Getters/setters: get fullName() { }; set fullName(value) { }. Classes are NOT hoisted like function declarations. Class expressions: const Animal = class { }.
Previous
What is the prototype chain in JavaScript?
Next
What is the difference between Object.assign and the spread operator for objects?