1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <head>createPersonClosureDestruct</head>
- <body>
- <script>
- const isUpperCase = (str) => str.toUpperCase() == str;
- let createPerson = function (name, surName) {
- let result = { 'name': name, 'surName': surName };
- result.getFullName = function () {
- return `${this.name} ${this.surName}`;
- }
- return result;
- }
- let createPersonClosureDestruct = function (obj) {
- let result = {};
- let { name = "Vasiya", surName = "Pupkin", fatherName = "Pupkovich", age = 17 } = obj;
- 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 a = createPersonClosureDestruct(createPerson("Вася Пупкин"))
- const b = createPersonClosureDestruct({ name: 'Николай', age: 75 })
- console.log(a.getFullName());
- console.log(b.getFullName());
- </script>
- </body>
|