Getters & Setters in JavaScript #40 | JavaScript Tutorial

preview_player
Показать описание
Getters & Setters in JavaScript #40 | JavaScript Tutorial

Courses:

HTML:

CSS:

Other videos

► How to Make Animated Portfolio Website Using HTML CSS & JS - Light & Dark Mode Website Tutorial:

► Responsive Personal Portfolio Website using HTML & CSS - How to Make a Website:

► Animated Portfolio Website Template in HTML CSS | Animated Personal Website - Responsive Design:

► How To Make A Website Using HTML CSS Bootstrap - Real Estate Website - Bootstrap Website Design:

► Responsive Ecommerce Website:

►CSS Button Hover Effect:

►CSS Loading Animation:

►Video Background Landing Page:

►Working JavaScript Clock:

►CSS Loading Animation Effects:

Instagram:
Рекомендации по теме
Комментарии
Автор

// getter (get) = special method that is used to access the value of a property.

// setter (set) = special method that allow you to update the value of a property with additional logic or validation.

class Person {
constructor(firstName, lastName) {
//underscore indicate a private property
this._firstName = firstName;
this._lastName = lastName;
}

// Getter for accessing the fullName
get fullName() {
return `${this._firstName} ${this._lastName}`;
}

// Setter for updating the first name with validation
set firstName(newFirstName) {
if (typeof newFirstName === 'string') {
this._firstName = newFirstName;
} else {
console.error('First name must be a string.');
}
}

set lastName(newLastName) {
if (typeof newLastName === 'string') {
this._lastName = newLastName;
} else {
console.error('Last name must be a string.');
}
}
}

const myPerson = new Person('John', 'Doe');




myPerson.firstName = 'Sam';


myPerson.firstName = 42;

webcontent