Conversation
| } | ||
| } | ||
|
|
||
| Object.defineProperty(target, propertyKey, { |
| let exampleInstance = new Example1(); | ||
| exampleInstance.email = "fkkldfjg"; // генерирует эксепшен | ||
| exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid | ||
| exampleInstance.email = "misha@mail.ru"; // выводит в консоль e-mail valid No newline at end of file |
| } | ||
|
|
||
| const test = new Calculator(10, 20); | ||
| test.exec(); |
There was a problem hiding this comment.
Это не паттерн декоратор.
https://refactoring.guru/ru/design-patterns/decorator/typescript/example
| const x = undefined; | ||
| const x = {m: 'some value'}; | ||
|
|
||
| console.log(getProperty(x, "m")); No newline at end of file |
|
|
||
| /**Класс описывает SelectBox контрол */ | ||
| class SelectBox extends Control<SelectItem> { | ||
| protected value: SelectItem; |
There was a problem hiding this comment.
Зачем переопределил? это поле имеет шаблонный тип в базовом классе.
| const typeInst: Control<R> = new type(); | ||
| const instance = this._collection.filter(element => element.type === typeInst.name); | ||
| return instance[0].instance; | ||
| } |
There was a problem hiding this comment.
Метод можно было сделать проще вот так
public getInstance<T extends Control>(type: new () => T): T {
const instance = this._collection.find(element => element.type === type.name);
return instance.instance as T;
}
| const instance = this._collection.filter(element => element.type === typeInst.name); | ||
| return instance[0].instance; | ||
| } | ||
|
|
| } | ||
| } | ||
|
|
||
| const example = new Example(ValueExample1, ValueExample2); |
There was a problem hiding this comment.
Не правильно.
Цель данной задачи состояла в том , что бы понять как работает декоратор свойств и полей класса. В данном случае ты в свойство пытаешься установить тип, зачем?
В поле надо устанавливать объект, т.е. проверять это надо было вот так:
const example = new Example();
example.propValueExample1 = new ValueExample1("21", 21);
example.propValueExample2 = new ValueExample2();
| } | ||
| } | ||
|
|
||
| Object.defineProperty(target, propertyName, { |
| return propertyValue; | ||
| }, | ||
| set(newVal: T) { | ||
| if (newVal === type) { |
There was a problem hiding this comment.
тут надо проверять тип передаваемого объекта вот так
newVal.constructor.name
|
Пока что 9 балов |
No description provided.