JavaScript Class Object Questions and Answers

  1. What is a class in JavaScript?
  • A blueprint for creating objects, encapsulating data and functions.
  1. How do you define a class?
  • class MyClass { constructor() { /*...*/ } }
  1. What is the purpose of the constructor?
  • To initialize object properties.
  1. Can a class have multiple constructors?
  • No, only one constructor is allowed. Use default parameters or method overloading patterns.
  1. What is inheritance in JavaScript?
  • A mechanism to create a new class from an existing class using extends.
  1. How do you inherit a class?
  • class ChildClass extends ParentClass {}
  1. What are static methods?
  • Methods called on the class itself rather than instances. Defined with static.
  1. How do you define a static method?
  • static myStaticMethod() { /*...*/ }
  1. What is the super keyword?
  • Used to call a parent class’s constructor or methods.
  1. How do you create an instance of a class?
    • const instance = new MyClass();
  2. What are object literals?
    • A way to define objects using curly braces: { key: value }.
  3. How do you access object properties?
    • Using dot notation: object.property or bracket notation: object['property'].
  4. What is this in a class?
    • A reference to the current instance of the class.
  5. Can you have private properties?
    • Yes, using # prefix: #privateProperty.
  6. What are getter and setter methods?
    • Methods to get or set property values. Defined with get and set.
  7. How do you define a getter?
    • get myProperty() { return this._myProperty; }
  8. How do you define a setter?
    • set myProperty(value) { this._myProperty = value; }
  9. What is a prototype?
    • An object from which other objects inherit properties.
  10. How do you add a method to a class?
    • Define it within the class body: method() { /*...*/ }.
  11. What is object destructuring?
    • A syntax to unpack values from objects: const { a, b } = obj;.
  12. How can you copy an object?
    • Using Object.assign({}, obj) or the spread operator: { ...obj }.
  13. What is the difference between shallow copy and deep copy?
    • Shallow copy duplicates the first level of properties; deep copy duplicates all levels.
  14. How do you check if a property exists in an object?
    • Using in operator: 'property' in obj or obj.hasOwnProperty('property').
  15. What is the purpose of Object.keys()?
    • To retrieve an array of an object’s own enumerable property names.
  16. What is a class expression?
    • A class defined as part of an expression: const MyClass = class { /*...*/ };.
  17. What are mixins?
    • Reusable pieces of code that can be added to classes to extend functionality.
  18. How do you create an object from a class?
    • Instantiate it using the new keyword.
  19. What is method chaining?
    • Calling multiple methods on the same object in a single statement.
  20. How do you define an abstract class in JavaScript?
    • By throwing an error in the constructor or specific methods.
  21. What is the instanceof operator?
    • A way to check if an object is an instance of a particular class.
  22. How do you prevent a class from being instantiated?
    • Use a private constructor or throw an error.
  23. What is the difference between a class and a function constructor?
    • Classes are syntactic sugar over function constructors, providing a clearer structure.
  24. What are enumerables in objects?
    • Properties that can be iterated over in a for...in loop.
  25. How do you freeze an object?
    • Using Object.freeze(obj) to make it immutable.
  26. What is Object.create()?
    • Creates a new object with a specified prototype object.
  27. What is a factory function?
    • A function that returns a new object.
  28. How can you extend built-in objects?
    • By adding methods or properties directly to their prototypes.
  29. What are class fields?
    • Properties declared within a class body, either public or private.
  30. What is the purpose of Object.entries()?
    • Returns an array of a given object’s own enumerable string-keyed property [key, value] pairs.
  31. How do you define default parameter values in methods?
    • method(param = defaultValue) { /*...*/ }.
  32. What is a singleton pattern?
    • A design pattern that restricts a class to a single instance.
  33. How do you define a subclass?
    • By using class SubClass extends SuperClass {}.
  34. What is the new keyword?
    • It creates a new instance of a class.
  35. How do you prevent inheritance in a class?
    • Using Object.preventExtensions() on the class prototype.
  36. What is a class decorator?
    • A function that modifies or enhances a class definition.
  37. What is the Object.prototype?
    • The base object from which all JavaScript objects inherit properties and methods.
  38. What is a circular reference in objects?
    • When an object references itself directly or indirectly.
  39. How can you serialize an object?
    • Using JSON.stringify(obj).
  40. What is a proxy in JavaScript?
    • An object that wraps another object to intercept and redefine fundamental operations.
  41. What is the purpose of Object.assign()?
    • To copy values from one or more source objects to a target object.

Related Posts

Top JavaScript String Functions with example

Top JavaScript String Functions with example

Top JavaScript String Functions with example

What is Redux ? How it Work ?

What is Redux ? How it Work ?

Understanding Redux: A Comprehensive Overview Redux is a predictable state management library often used in JavaScript applications, particularly with React. It was created to manage the state…

JavaScript Interview Questions with Answers for Fresher

JavaScript Interview Questions with Answers for Fresher

Here’s a list of 50 common JavaScript questions along with their answers to help you get started or refresh your knowledge: 1. What is JavaScript? JavaScript is…

Leave a Reply

Your email address will not be published. Required fields are marked *