Classes in JavaScript: Syntactic Sugar or Real Abstraction? Quiz

Explore how JavaScript classes relate to prototypes, constructor functions, inheritance, and encapsulation in modern JavaScript. This easy quiz tests key concepts and features that help make your code clearer and more maintainable.

  1. What is the primary purpose of the constructor method in a JavaScript class?

    What is the primary purpose of the constructor method in a JavaScript class?

    1. To store all methods privately
    2. To initialize instance properties when a new object is created
    3. To inherit properties from another class
    4. To define static methods for utility functions

    Explanation: The constructor method initializes properties for each new instance of a class. Static methods are defined using the 'static' keyword. Inheritance is handled by 'extends' and 'super', not the constructor itself. Methods are not stored privately in the constructor; they exist on the prototype.

  2. Where are methods defined in a JavaScript class actually stored?

    Where are methods defined in a JavaScript class actually stored?

    1. Inside each instance object
    2. On the class's prototype
    3. In the constructor function
    4. As private fields

    Explanation: Class methods are stored on the prototype, making them shared between instances and conserving memory. They are not placed inside each instance, nor directly in the constructor, and private fields are a separate feature.

  3. Which syntax enables inheritance between two JavaScript classes?

    Which syntax enables inheritance between two JavaScript classes?

    1. inherit
    2. implements
    3. derive
    4. extends

    Explanation: The 'extends' keyword allows one class to inherit properties and methods from another. 'implements' is not used in JavaScript for class inheritance; 'inherit' and 'derive' are not valid JavaScript syntax for inheritance.

  4. How do static methods differ from regular methods in JavaScript classes?

    How do static methods differ from regular methods in JavaScript classes?

    1. They can access instance properties directly
    2. They run only once per program
    3. They are always private
    4. They belong to the class itself, not to instances

    Explanation: Static methods are called on the class itself and not on its instances. They do not automatically access instance properties and are not necessarily private. They are not executed just once per program.

  5. What feature do private fields (e.g., #count) in JavaScript classes provide?

    What feature do private fields (e.g., #count) in JavaScript classes provide?

    1. Faster prototype chain resolution
    2. Global property access
    3. True encapsulation of instance properties
    4. Automatic method inheritance

    Explanation: Private fields using '#' provide true encapsulation, making those properties accessible only within the class. They do not affect prototype chain speed, do not enable method inheritance by themselves, and do not make properties globally accessible.

  6. Why might using classes in JavaScript be unnecessary for simple objects?

    Why might using classes in JavaScript be unnecessary for simple objects?

    1. Factory functions or simple patterns can be clearer and less complex
    2. Inheritance is mandatory in every class
    3. Classes cannot have instance properties
    4. Classes lack support for private fields

    Explanation: For small or simple objects, factory functions may be easier to use and read. Classes do support instance properties and private fields, and inheritance is not mandatory in every class.

  7. What does it mean that JavaScript classes are 'syntactic sugar' over prototypes?

    What does it mean that JavaScript classes are 'syntactic sugar' over prototypes?

    1. Classes have no relation to prototypes
    2. Classes always enforce strict encapsulation
    3. Classes cannot be used to create objects
    4. Classes provide a clearer syntax but use prototype-based inheritance under the hood

    Explanation: JavaScript classes offer a familiar and concise syntax, but they work by leveraging the existing prototype system. They are closely tied to prototypes, can be used to create objects, and do not enforce strict encapsulation by default.

  8. When is it particularly useful to use JavaScript classes?

    When is it particularly useful to use JavaScript classes?

    1. When avoiding any form of code reuse
    2. When dynamic code evaluation is required
    3. When building complex objects requiring methods, inheritance, or readable code structure
    4. When only single-purpose utility functions are needed

    Explanation: Classes are helpful for organizing complex objects, supporting method sharing, inheritance, and improving team readability. For simple utilities, classes can be unnecessary; avoiding code reuse or dynamic evaluation does not require the use of classes.