index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // ДЗ: Функциональное Объектно-Ориентированное программирование
  2. // задание 1 Конструктор лица
  3. {
  4. function Person(name, surname) {
  5. this.name = name;
  6. this.surname = surname;
  7. this.getFullName = function () {
  8. return `${this.name} ${this.surname}`;
  9. };
  10. }
  11. }
  12. {
  13. // пример использования:
  14. const a = new Person('Вася', 'Пупкин');
  15. console.log(a.getFullName()); // Вася Пупкин
  16. a.fatherName = 'Иванович';
  17. console.log(a.getFullName()); // Вася Иванович Пупкин
  18. const b = new Person('Анна', 'Иванова');
  19. console.log(b.getFullName()); // Анна Иванова
  20. const c = new Person('Елизавета', 'Петрова');
  21. console.log(c.getFullName()); // Елизавета Петрова
  22. }
  23. // задание 2 Прототип человека
  24. {
  25. function Person(name, surname) {
  26. this.name = name;
  27. this.surname = surname;
  28. }
  29. Person.prototype.getFullName = function () {
  30. return `${this.name} ${this.surname}`;
  31. };
  32. }
  33. {
  34. // использование:
  35. const a = new Person('Вася', 'Пупкин');
  36. console.log(a.getFullName()); // Вася Пупкин
  37. a.fatherName = 'Иванович';
  38. console.log(a.getFullName()); // Вася Иванович Пупкин
  39. const b = new Person('Анна', 'Иванова');
  40. console.log(b.getFullName()); // Анна Иванова
  41. const c = new Person('Елизавета', 'Петрова');
  42. console.log(c.getFullName()); // Елизавета Петрова
  43. }
  44. // задание 3 Магазин
  45. {
  46. function Store(reducer, state) {
  47. this.state = state;
  48. this.cbs = [];
  49. this.dispatch = (action) => {
  50. this.state = reducer(this.state, action);
  51. this.cbs.forEach((cb) => cb());
  52. };
  53. this.getState = () => this.state;
  54. this.subscribe = (cb) => {
  55. this.cbs.push(cb);
  56. return () => (this.cbs = this.cbs.filter((c) => c !== cb));
  57. };
  58. }
  59. }
  60. {
  61. const reducer = (state, action) => {
  62. // обработка action и возврат нового состояния
  63. return newState;
  64. };
  65. const store = new Store(reducer, {});
  66. store.subscribe(() => {
  67. console.log('State changed:', store.getState());
  68. });
  69. store.dispatch({ type: 'INCREMENT' }); // State changed: { count: 1 }
  70. store.dispatch({ type: 'DECREMENT' }); // State changed: { count: 0 }
  71. }
  72. // задание 4 Пароль
  73. {
  74. function Password(parent, open) {
  75. this.input = document.createElement('input');
  76. this.input.type = open ? 'text' : 'password';
  77. this.input.addEventListener('input', () => this.onChange(this.input.value));
  78. this.checkbox = document.createElement('input');
  79. this.checkbox.type = 'checkbox';
  80. this.checkbox.checked = open;
  81. this.checkbox.addEventListener('change', () => this.setOpen(this.checkbox.checked));
  82. parent.append(this.input, this.checkbox);
  83. this.setValue = (value) => {
  84. this.input.value = value;
  85. };
  86. this.getValue = () => this.input.value;
  87. this.setOpen = (open) => {
  88. this.input.type = open ? 'text' : 'password';
  89. this.checkbox.checked = open;
  90. this.onOpenChange(open);
  91. };
  92. this.getOpen = () => this.checkbox.checked;
  93. }
  94. }
  95. // задание 5 ЛогинФорма
  96. {
  97. const loginForm = document.createElement('form');
  98. const loginInput = document.createElement('input');
  99. loginInput.type = 'text';
  100. loginInput.placeholder = 'Логин';
  101. loginForm.append(loginInput);
  102. const password = new Password(loginForm, false);
  103. password.onChange = () => (loginButton.disabled = !(loginInput.value && password.getValue()));
  104. const loginButton = document.createElement('button');
  105. loginButton.textContent = 'Войти';
  106. loginButton.disabled = true;
  107. loginForm.append(loginButton);
  108. document.body.append(loginForm);
  109. }
  110. // задание 6 Конструктор формы входа
  111. {
  112. function LoginForm(parent) {
  113. this.form = document.createElement('form');
  114. this.loginInput = document.createElement('input');
  115. this.loginInput.type = 'text';
  116. this.loginInput.placeholder = 'Логин';
  117. this.form.append(this.loginInput);
  118. this.password = new Password(this.form, false);
  119. this.password.onChange = () => this.updateButtonState();
  120. this.button = document.createElement('button');
  121. this.button.textContent = 'Войти';
  122. this.button.disabled = true;
  123. this.form.append(this.button);
  124. parent.append(this.form);
  125. this.setLogin = (login) => {
  126. this.loginInput.value = login;
  127. this.updateButtonState();
  128. };
  129. this.getLogin = () => this.loginInput.value;
  130. this.setPassword = (password) => {
  131. this.password.setValue(password);
  132. this.updateButtonState();
  133. };
  134. this.getPassword = () => this.password.getValue();
  135. this.updateButtonState = () => {
  136. this.button.disabled = !(this.loginInput.value && this.password.getValue());
  137. };
  138. this.onSubmit = () => {};
  139. this.form.addEventListener('submit', (event) => {
  140. event.preventDefault();
  141. this.onSubmit();
  142. });
  143. }
  144. }
  145. // задание 7 Подтвердить пароль
  146. {
  147. function PasswordChecker(parent) {
  148. this.form = document.createElement('form');
  149. this.password1 = new Password(this.form, false);
  150. this.password1.onChange = () => this.updateValidity();
  151. this.form.append(this.password1.form);
  152. this.password2 = new Password(this.form, false);
  153. this.password2.onChange = () => this.updateValidity();
  154. this.form.append(this.password2.form);
  155. parent.append(this.form);
  156. this.updateValidity = () => {
  157. const isValid = this.password1.getValue() === this.password2.getValue();
  158. this.password1.form.classList.toggle('invalid', !isValid);
  159. this.password2.form.classList.toggle('invalid', !isValid);
  160. };
  161. this.password1.onOpenChange = (isOpen) => {
  162. if (isOpen) {
  163. this.password2.form.style.display = 'none';
  164. } else {
  165. this.password2.form.style.display = 'block';
  166. }
  167. };
  168. }
  169. }