From 7afd0acf418f70c36ffbc79af786723ddc30e71a Mon Sep 17 00:00:00 2001 From: deyantomov Date: Mon, 29 Jul 2024 21:58:12 +0300 Subject: [PATCH 1/3] added a couple of interview questions about ES6 classes --- en/interview-questions/intermediate-level.md | 72 +++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/en/interview-questions/intermediate-level.md b/en/interview-questions/intermediate-level.md index 0b909a82..985bfd82 100644 --- a/en/interview-questions/intermediate-level.md +++ b/en/interview-questions/intermediate-level.md @@ -287,4 +287,74 @@ if ([] === true) { } ``` -Understanding truthy and falsy values allows us to write more concise and expressive code, especially when dealing with conditional logic. We can use this behavior to write shorter and more readable code when evaluating conditions and choosing between two values or actions. \ No newline at end of file +Understanding truthy and falsy values allows us to write more concise and expressive code, especially when dealing with conditional logic. We can use this behavior to write shorter and more readable code when evaluating conditions and choosing between two values or actions. + +## 8. Classes + +### 8.1. What are JavaScript classes? + +**Answer:** Classes are templates for creating objects. They encapsulate data and logic that works with the data. Before ES6, the concept of classes in JavaScript was nonexistent. Similar behavior was achieved using constructor functions and prototypes. Classes in JavaScript are built on prototypes but have some unique syntax and semantics, which offer a cleaner way to create and manage objects. + +### 8.2. Class members and class properties + +**Answer:** Class members refer to the methods and fields within a class. + + + i. Methods + - Functions that are defined within the class. + - They can operate on fields and perform actions with the data. + - They define the behavior of the class. + + + ii. Fields + - Variables that are defined within the class. + - They hold data specific to the class instance. + - They define the state of the class. + +Properties refer to the getter and setter within a class, which provide a way to control access to the fields of a class. The getter and setter are property bindings to a function that will be called when the property is accessed. + + + i. Getter + - A getter is a method that gets the value of a field. + - It must have exactly zero parameters. + - It must return a value. + - It is defined using the ```get``` keyword. + + + ii. Setter + - A setter is a method that sets the value of a field. + - It must have exactly one parameter. + - It is defined using the ```set``` keyword. + +They are used to encapsulate the data, ensuring only certain fields can be accessed or modified in a specific way. + +### 8.3. Explain constructor functions + +**Answer:** The constructor is a special method of a class that is called upon the initialization of the class. It is used to initialize the object's properties and perform any setup that is necessary when the object is created. + + + i. Declaration + - The constructor method is declared using the ```constructor``` keyword within the class body. + - Each class can only have one constructor method. + - The constructor cannot be declared as an async method. + + + ii. Initialization + - The constructor can accept parameters that are used to initialize the instance's properties. + - It sets up the initial state by assigning values to properties. + + + iii. Inheritance + - In derived classes (classes that extend another class), the ```super``` keyword is used within the child's constructor to call the constructor of the parent class. + - The ```super``` call must be made before accessing any properties in the constructor of a derived class. + +Example of constructor inheritance + +```javascript +class Animal { + constructor(species) { + this.species = species; + } +} + +class Dog extends Animal { + constructor(breed) { + // Call the parent class constructor + super("Dog"); + this.breed = breed; + } +} +``` From 76b84d832b85dfc7d0f447204af56667f208bea6 Mon Sep 17 00:00:00 2001 From: deyantomov Date: Wed, 31 Jul 2024 10:35:59 +0300 Subject: [PATCH 2/3] added more questions and better answers --- en/interview-questions/intermediate-level.md | 136 +++++++++++++++---- 1 file changed, 111 insertions(+), 25 deletions(-) diff --git a/en/interview-questions/intermediate-level.md b/en/interview-questions/intermediate-level.md index 985bfd82..9632a620 100644 --- a/en/interview-questions/intermediate-level.md +++ b/en/interview-questions/intermediate-level.md @@ -293,9 +293,9 @@ Understanding truthy and falsy values allows us to write more concise and expres ### 8.1. What are JavaScript classes? -**Answer:** Classes are templates for creating objects. They encapsulate data and logic that works with the data. Before ES6, the concept of classes in JavaScript was nonexistent. Similar behavior was achieved using constructor functions and prototypes. Classes in JavaScript are built on prototypes but have some unique syntax and semantics, which offer a cleaner way to create and manage objects. +**Answer:** Classes are templates for creating objects. They encapsulate data and logic that works with the data. -### 8.2. Class members and class properties +### 8.2. What are class members? **Answer:** Class members refer to the methods and fields within a class. @@ -303,20 +303,36 @@ Understanding truthy and falsy values allows us to write more concise and expres - Functions that are defined within the class. - They can operate on fields and perform actions with the data. - They define the behavior of the class. - + ii. Fields - Variables that are defined within the class. - They hold data specific to the class instance. - They define the state of the class. -Properties refer to the getter and setter within a class, which provide a way to control access to the fields of a class. The getter and setter are property bindings to a function that will be called when the property is accessed. +### 8.3. Explain the types of access modifiers and their purpose in JavaScript + +**Answer:** There are three types of access modifiers which are used to encapsulate information. + + + Public + - Can be accessed from anywhere. + + Private + - Indicated by prefixing them with the ```#``` symbol. + - Cannot be accessed from instances or child classes. + - Only available within the class itself. + - Useful for information hiding. + + Protected + - Indicated by prefixing them with the ```_``` symbol. + - Can be accessed from the within the class and any class that inherits from it. + - Useful for sharing state between classes. + + +### 8.4. What are class properties? +**Answer:** Properties refer to the getter and setter within a class, which provide a way to control access to the fields of a class. The getter and setter are property bindings to a function that will be called when the property is accessed. + i. Getter - A getter is a method that gets the value of a field. - It must have exactly zero parameters. - It must return a value. - It is defined using the ```get``` keyword. - + ii. Setter - A setter is a method that sets the value of a field. - It must have exactly one parameter. @@ -324,37 +340,107 @@ Properties refer to the getter and setter within a class, which provide a way to They are used to encapsulate the data, ensuring only certain fields can be accessed or modified in a specific way. -### 8.3. Explain constructor functions +Example of encapsulation using getters and setters: -**Answer:** The constructor is a special method of a class that is called upon the initialization of the class. It is used to initialize the object's properties and perform any setup that is necessary when the object is created. +```javascript + class Task { + #id; + #description; + + constructor(id, description, ) { + // Use setters instead of directly modifying the private fields + this.id = id; + this.description = description; + } + + // Use getters to read the values + get id() { + return this.#id; + } + + // ... + + // Use setters to validate data before writing + set id(id) { + // ...validate new ID + this.#id = id; + } + } +``` + +### 8.5. Explain class constructors + +**Answer:** The constructor is a special method of a class called upon the initialization of that class. It is used to initialize the object's properties and perform any setup that is necessary when the object is created. + i. Declaration - The constructor method is declared using the ```constructor``` keyword within the class body. - Each class can only have one constructor method. - - The constructor cannot be declared as an async method. - + - The constructor cannot be an async method. + - The constructor cannot be a private method. + ii. Initialization - The constructor can accept parameters that are used to initialize the instance's properties. - It sets up the initial state by assigning values to properties. - + iii. Inheritance - In derived classes (classes that extend another class), the ```super``` keyword is used within the child's constructor to call the constructor of the parent class. - - The ```super``` call must be made before accessing any properties in the constructor of a derived class. + - The ```super``` call must be made before accessing any properties in the constructor of a derived class. Example of constructor inheritance: -Example of constructor inheritance + ```javascript + class Animal { + constructor(species) { + this.species = species; + } + } -```javascript -class Animal { - constructor(species) { - this.species = species; + class Person extends Animal { + constructor(name) { + // Call the parent class constructor + super("Human"); + this.name = name; + } } -} + ``` + - If no constructor is explicitly declared, a default one will be given to the class. -class Dog extends Animal { - constructor(breed) { - // Call the parent class constructor - super("Dog"); - this.breed = breed; - } -} -``` + - Base class - If the class doesn't extend any other class, a default constructor will be assigned to it: + + ```javascript + constructor() {} + ``` + + - Child class - If the class extends another class, it will inherit its constructor + + ```javascript + class Vehicle { + constructor(type) { + this.type = type; + } + + move() { + return `The ${this.type} is moving`; + } + } + + class Car extends Vehicle { + #model; + + set model(model) { + this.#model = model; + } + + drive() { + `Driving ${this.#model}`; + } + } + ``` + + Even though the constructor is not explicitly declared, the class Car inherits it from the Vehicle class: + + ```javascript + constructor(type) { + super(type); + } + ``` + +### 8.6. Static members vs Instance members + +**Answer:** By default, properties and methods that we define inside a class, belong to each instance of the class that we create. We can also assign members to the class itself. Such members are called static, and are declared using the ```static``` keyword. They cannot be directly accessed on instances of the class. \ No newline at end of file From 55b56f7f523e9a2ce596846191c8bb88a5de2d90 Mon Sep 17 00:00:00 2001 From: deyantomov Date: Wed, 31 Jul 2024 11:15:39 +0300 Subject: [PATCH 3/3] fixed minor mistakes --- en/interview-questions/intermediate-level.md | 75 +++++++++----------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/en/interview-questions/intermediate-level.md b/en/interview-questions/intermediate-level.md index 9632a620..5e70d388 100644 --- a/en/interview-questions/intermediate-level.md +++ b/en/interview-questions/intermediate-level.md @@ -315,12 +315,12 @@ Understanding truthy and falsy values allows us to write more concise and expres + Public - Can be accessed from anywhere. + Private - - Indicated by prefixing them with the ```#``` symbol. + - Indicated by prefixing members with the ```#``` symbol. - Cannot be accessed from instances or child classes. - Only available within the class itself. - Useful for information hiding. + Protected - - Indicated by prefixing them with the ```_``` symbol. + - Indicated by prefixing members with the ```_``` symbol. - Can be accessed from the within the class and any class that inherits from it. - Useful for sharing state between classes. @@ -365,6 +365,8 @@ Example of encapsulation using getters and setters: // ...validate new ID this.#id = id; } + + // ... } ``` @@ -393,54 +395,47 @@ Example of encapsulation using getters and setters: class Person extends Animal { constructor(name) { - // Call the parent class constructor - super("Human"); + super("Human"); // Call the parent class constructor this.name = name; } } ``` - - If no constructor is explicitly declared, a default one will be given to the class. - - - Base class - If the class doesn't extend any other class, a default constructor will be assigned to it: - - ```javascript - constructor() {} - ``` - - - Child class - If the class extends another class, it will inherit its constructor - - ```javascript - class Vehicle { - constructor(type) { - this.type = type; - } - - move() { - return `The ${this.type} is moving`; - } + + iv. Default constructor - If no constructor is explicitly declared, a default one will be given to the class. + - Base class - If the class doesn't extend any other class, a default constructor will be assigned to it: + ```javascript + constructor() {} + ``` + - Child class - If the class extends another class, it will inherit its constructor + ```javascript + class Vehicle { + constructor(type) { + this.type = type; } - class Car extends Vehicle { - #model; - - set model(model) { - this.#model = model; - } - - drive() { - `Driving ${this.#model}`; - } + move() { + return `The ${this.type} is moving`; } - ``` + } - Even though the constructor is not explicitly declared, the class Car inherits it from the Vehicle class: + class Car extends Vehicle { + #model; - ```javascript - constructor(type) { - super(type); + set model(model) { + this.#model = model; + } + + drive() { + `Driving ${this.#model}`; } - ``` + } + ``` + Even though the constructor is not explicitly declared, the class Car inherits it from the Vehicle class: + ```javascript + constructor(type) { + super(type); + } + ``` ### 8.6. Static members vs Instance members -**Answer:** By default, properties and methods that we define inside a class, belong to each instance of the class that we create. We can also assign members to the class itself. Such members are called static, and are declared using the ```static``` keyword. They cannot be directly accessed on instances of the class. \ No newline at end of file +**Answer:** By default, properties and methods which we define inside a class belong to each instance of the class that we create. We can also assign members to the class itself. Such members are called static and are declared using the ```static``` keyword. They cannot be directly accessed on instances of the class. \ No newline at end of file