hw11_06_personForm_closures.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <head>person Form</head>
  2. <body>
  3. <script>
  4. /*const addElement = (domEl, value, onInputFunc) => {
  5. const input = document.createElement('input');
  6. input.value = value;
  7. input.oninput = onInputFunc;
  8. domEl.append(input);
  9. }*/
  10. const personForm = (domEl, obj) => {
  11. /*addElement(
  12. domEl,
  13. obj.getName(),
  14. () => {
  15. obj.setName(nameInput.value);
  16. fullNameInput.value = obj.getFullName();
  17. });
  18. addElement(
  19. domEl,
  20. obj.getSurName(),
  21. () => {
  22. obj.setSurName(nameInput.value);
  23. fullNameInput.value = obj.getFullName();
  24. });
  25. addElement(
  26. domEl,
  27. obj.getFatherName(),
  28. () => {
  29. obj.setFatherName(nameInput.value);
  30. fullNameInput.value = obj.getFullName();
  31. });
  32. addElement(
  33. domEl,
  34. obj.getAge(),
  35. () => obj.setAge(ageInput.value));
  36. addElement(
  37. domEl,
  38. obj.getFullName(),
  39. () => {
  40. obj.setFullName(fullNameInput.value);
  41. nameInput.value = obj.getName();
  42. surNameInput.value = obj.getSurName();
  43. fatherNameInput.value = obj.getFatherName();
  44. });*/
  45. const nameInput = document.createElement('input');
  46. const surNameInput = document.createElement('input');
  47. const fatherNameInput = document.createElement('input');
  48. const ageInput = document.createElement('input');
  49. const fullNameInput = document.createElement('input');
  50. domEl.append(nameInput);
  51. nameInput.value = obj.getName();
  52. domEl.append(surNameInput);
  53. surNameInput.value = obj.getSurName();
  54. domEl.append(fatherNameInput);
  55. fatherNameInput.value = obj.getFatherName();
  56. domEl.append(fullNameInput);
  57. fullNameInput.value = obj.getFullName();
  58. domEl.append(ageInput);
  59. ageInput.value = obj.getAge();
  60. nameInput.oninput = () => {
  61. obj.setName(nameInput.value);
  62. fullNameInput.value = obj.getFullName();
  63. }
  64. surNameInput.oninput = () => {
  65. obj.setSurName(surNameInput.value);
  66. fullNameInput.value = obj.getFullName();
  67. }
  68. fatherNameInput.oninput = () => {
  69. obj.setFatherName(fatherNameInput.value);
  70. fullNameInput.value = obj.getFullName();
  71. }
  72. ageInput.oninput = () => obj.setAge(ageInput.value);
  73. fullNameInput.oninput = () => {
  74. obj.setFullName(fullNameInput.value);
  75. nameInput.value = obj.getName();
  76. surNameInput.value = obj.getSurName();
  77. fatherNameInput.value = obj.getFatherName();
  78. }
  79. }
  80. const isUpperCase = (str) => str.toUpperCase() == str;
  81. let createPersonClosure = function (name, surName) {
  82. let result = {};
  83. let age = 17;
  84. let fatherName = "Пупкович";
  85. result.getName = () => name;
  86. result.getSurName = () => surName;
  87. result.getFullName = function () {
  88. return `${result.getName()} ${result.getFatherName()} ${result.getSurName()} `;
  89. }
  90. result.setFullName = (newFullName) => {
  91. const arrFullName = newFullName.split(' ');
  92. result.setFatherName(arrFullName[1]);
  93. result.setSurName(arrFullName[2]);
  94. result.setName(arrFullName[0]);
  95. }
  96. result.setName = (newName) => {
  97. if (isUpperCase(newName[0])) {
  98. name = newName;
  99. }
  100. else
  101. alert("wrong Name format");
  102. }
  103. result.setSurName = (newSurName) => {
  104. if (isUpperCase(newSurName[0])) {
  105. surName = newSurName;
  106. }
  107. else
  108. alert("wrong Surname format");
  109. }
  110. result.setFatherName = (newFatherName) => {
  111. if (isUpperCase(newFatherName[0])) {
  112. fatherName = newFatherName;
  113. }
  114. else
  115. alert("wrong Fathername format");
  116. }
  117. result.getAge = () => age;
  118. result.setAge = (newAge) => {
  119. if (newAge >= 0 && newAge <= 100)
  120. age = newAge;
  121. else
  122. alert("wrong age: " + newAge);
  123. }
  124. result.getFatherName = () => fatherName;
  125. return result;
  126. }
  127. const person = createPersonClosure("Анна", "Иванова");
  128. personForm(document.body, person);
  129. </script>
  130. </body>