Здесь показаны различия между двумя версиями данной страницы.
| Предыдущая версия справа и слева Предыдущая версия | |||
|
typescript:oop:getters_and_setters [2023/02/12 21:52] werwolf |
typescript:oop:getters_and_setters [2023/02/12 21:58] (текущий) werwolf |
||
|---|---|---|---|
| Строка 85: | Строка 85: | ||
| </code> | </code> | ||
| - | + | <note> | |
| - | Notice that the call to the setter doesn’t have parentheses like a regular method. When you call person.age, the age setter method is invoked. If you assign an invalid age value, the setter will throw an error: | + | Обратите внимание, что вызов setter не содержит круглых скобок, как в обычном методе. Когда вы вызываете person.age, вызывается метод setter возраста. Если вы присвоите недопустимое значение возраста, setter выдаст сообщение об ошибке: |
| + | </note> | ||
| <code javascript> | <code javascript> | ||
| Строка 98: | Строка 99: | ||
| </code> | </code> | ||
| - | When you access the person.age, the age getter is invoked. | + | Когда вы обращаетесь к person.age, вызывается getter возраста. |
| <code javascript> | <code javascript> | ||
| Строка 104: | Строка 106: | ||
| </code> | </code> | ||
| - | The following adds the getters and setters to the firstName and lastName properties. | + | Следующее добавляет методы getters и setters к свойствам FirstName и LastName. |
| <code javascript> | <code javascript> | ||
| Строка 151: | Строка 154: | ||
| </code> | </code> | ||
| - | ===== More TypeScript Getters/Setters Examples ===== | + | ===== Дополнительные примеры ===== |
| - | As you can see from the code, the setters are useful when you want to validate the data before assigning it to the properties. In addition, you can perform complex logic. | + | Как вы можете видеть из кода, setters полезны, когда вы хотите проверить данные перед присвоением их свойствам. Кроме того, вы можете выполнять сложную логику. |
| - | The following shows how to create the fullname getter and setter. | + | Ниже показано, как создать полное имя getter and setter. |
| <code javascript> | <code javascript> | ||
| Строка 175: | Строка 179: | ||
| </code> | </code> | ||
| - | How it works. | + | Как это работает. |
| - | * The getter method returns the concatenation of the first name and last name. | + | * getter возвращает объединение имени и фамилии. |
| - | * The setter method accepts a string as the full name with the format: first last and assign the first part to the first name property and second part to the last name property. | + | * Метод setter принимает строку в качестве полного имени в формате: first last и присваивает первую часть свойству first name, а вторую часть свойству last name. |
| Now, you can access the fullname setter and getter like a regular class property: | Now, you can access the fullname setter and getter like a regular class property: | ||
| + | Теперь вы можете получить доступ к setter и getter полного имени как к обычному свойству класса: | ||
| <code javascript> | <code javascript> | ||