hw11_03_create person destruct.html 2.4 KB

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