hw11_02_createPersonClosure.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <head>createPersonClosure</head>
  2. <body>
  3. <script>
  4. let createPersonClosure = function (name, surName) {
  5. let result = {};
  6. let age = 17;
  7. let fatherName = "Пупкович";
  8. result.getName = () => name;
  9. result.getSurName = () => surName;
  10. result.getFullName = function () {
  11. return `${result.getName()} ${result.getSurName()}`;
  12. }
  13. result.setFullName = (newFullName) => {
  14. const arrFullName = newFullName.split(' ');
  15. result.setFatherName(arrFullName[2]);
  16. result.setSurName(arrFullName[0]);
  17. result.setName(arrFullName[1]);
  18. }
  19. result.setName = (newName) => {
  20. if (isUpperCase(newName[0])) {
  21. name = newName;
  22. }
  23. else
  24. alert("wrong Name format");
  25. }
  26. result.setSurName = (newSurName) => {
  27. if (isUpperCase(newSurName[0])) {
  28. surName = newSurName;
  29. }
  30. else
  31. alert("wrong Surname format");
  32. }
  33. result.setFatherName = (newFatherName) => {
  34. if (isUpperCase(newFatherName[0])) {
  35. fatherName = newFatherName;
  36. }
  37. else
  38. alert("wrong Fathername format");
  39. }
  40. result.getAge = () => age;
  41. result.setAge = (newAge) => {
  42. if (newAge >= 0 && newAge <= 100)
  43. age = newAge;
  44. else
  45. alert("wrong age: " + newAge);
  46. }
  47. result.getFatherName = () => fatherName;
  48. return result;
  49. }
  50. const isUpperCase = (str) => str.toUpperCase() == str;
  51. const a = createPersonClosure("Вася", "Пупкин");
  52. const b = createPersonClosure("Анна", "Иванова");
  53. console.log(a.getName());
  54. a.setAge(15);
  55. console.log(a.getAge());
  56. a.setAge(150)
  57. b.setFullName("Петрова Анна Николаевна");
  58. console.log(b.getFatherName());
  59. </script>
  60. </body>