123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <head>createPersonClosure</head>
- <body>
- <script>
- let createPersonClosure = function (name, surName) {
- let result = {};
- let age = 17;
- let fatherName = "Пупкович";
- result.getName = () => name;
- result.getSurName = () => surName;
- result.getFullName = function () {
- return `${result.getName()} ${result.getSurName()}`;
- }
- result.setFullName = (newFullName) => {
- const arrFullName = newFullName.split(' ');
- result.setFatherName(arrFullName[2]);
- result.setSurName(arrFullName[0]);
- result.setName(arrFullName[1]);
- }
- result.setName = (newName) => {
- if (isUpperCase(newName[0])) {
- name = newName;
- }
- else
- alert("wrong Name format");
- }
- result.setSurName = (newSurName) => {
- if (isUpperCase(newSurName[0])) {
- surName = newSurName;
- }
- else
- alert("wrong Surname format");
- }
- result.setFatherName = (newFatherName) => {
- if (isUpperCase(newFatherName[0])) {
- fatherName = newFatherName;
- }
- else
- alert("wrong Fathername format");
- }
- result.getAge = () => age;
- result.setAge = (newAge) => {
- if (newAge >= 0 && newAge <= 100)
- age = newAge;
- else
- alert("wrong age: " + newAge);
- }
- result.getFatherName = () => fatherName;
- return result;
- }
- const isUpperCase = (str) => str.toUpperCase() == str;
- const a = createPersonClosure("Вася", "Пупкин");
- const b = createPersonClosure("Анна", "Иванова");
- console.log(a.getName());
- a.setAge(15);
- console.log(a.getAge());
- a.setAge(150)
- b.setFullName("Петрова Анна Николаевна");
- console.log(b.getFatherName());
- </script>
- </body>
|