Junior — Middle
Explain what a method for obtaining a property value in an object is.
sobes.tech AI
Answer from AI
The method of obtaining a property value in an object is a way to access data stored inside the object. In object-oriented programming, object properties are usually encapsulated, and special methods called getters are used to read them. Getters allow controlling access to internal data, for example, returning the property value or performing additional logic when retrieving it.
Example in JavaScript:
const person = {
firstName: 'Ivan',
lastName: 'Ivanov',
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
};
console.log(person.getFullName()); // Ivan Ivanov
Here, the getFullName method serves to obtain a value based on the object's properties.