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