### Initialize ```javascript function Person(name, surname, age, sex, salary, married){ this.name = name; this.surname = surname; this.age = age; this.sex = sex; this.salary = salary; this.married = married; } var objPerson = new Person("pasha","efimenko","22","male","0",false); console.log(objPerson.name); ``` ### Closure getters and setters ```javascript function Person(name, surname, age, sex, salary, married){ this.setName = function(newName){ if(typeof newName === "string"){ name = newName; } return name; } this.getName = function(){ return name; } this.setSurname = function(newSurname){ if(typeof newSurname === "string"){ surname = newSurname; } return surname; } this.getSurname = function(){ return surname; } this.setAge = function(newAge){ if(typeof newAge === "number"){ age = newAge; } return age; } this.getAge = function(){ return age; } this.setSex = function(newSex){ if(typeof newSex === "string"){ sex = newSex; } return sex; } this.getSex = function(){ return sex; } this.setSalary = function(newSalary){ if(typeof newSalary === "number"){ salary = newSalary; } return salary; } this.getSalary = function(){ return salary; } this.setMarried = function(newMarried){ if(typeof newMarried === "boolean"){ married = newMarried; } return married; } this.getMarried = function(){ return married; } } var objPerson = new Person("pasha","efimenko",22,"male",0,false); console.log(objPerson.getName()); console.log(objPerson.setName("Sasha")); console.log(objPerson.getName()); console.log(objPerson.getSurname()); console.log(objPerson.setSurname("Kovalenko")); console.log(objPerson.getSurname()); console.log(objPerson.getAge()); console.log(objPerson.setAge(40)); console.log(objPerson.getAge()); console.log(objPerson.getSex()); console.log(objPerson.setSex("female")); console.log(objPerson.getSex()); console.log(objPerson.getSalary()); console.log(objPerson.setSalary(1000)); console.log(objPerson.getSalary()); console.log(objPerson.getMarried()); console.log(objPerson.setMarried(true)); console.log(objPerson.getMarried()); ``` ### setFather, getFatherName, addChild ```javascript function Person(name, surname, age, sex, salary, married){ var childs = []; this.setName = function(newName){ if(typeof newName === "string"){ name = newName; } return name; } this.getName = function(){ return name; } this.setSurname = function(newSurname){ if(typeof newSurname === "string"){ surname = newSurname; } return surname; } this.getSurname = function(){ return surname; } this.setAge = function(newAge){ if(typeof newAge === "number"){ age = newAge; } return age; } this.getAge = function(){ return age; } this.setSex = function(newSex){ if(typeof newSex === "string"){ sex = newSex; } return sex; } this.getSex = function(){ return sex; } this.setSalary = function(newSalary){ if(typeof newSalary === "number"){ salary = newSalary; } return salary; } this.getSalary = function(){ return salary; } this.setMarried = function(newMarried){ if(typeof newMarried === "boolean"){ married = newMarried; } return married; } this.getMarried = function(){ return married; } this.setFather = function(obj){ //setFather this.father = obj; return this.father; } this.getFatherName = function(){ //getFatherName if(this.getSex() === "male"){ this.fatherName = this.father.getName() + "ovich"; } if(this.getSex() === "female"){ this.fatherName = this.father.getName() + "ovna"; } return this.fatherName } this.addChild = function(obj){ //addChild childs.push(obj); return childs.length; } } var fatherPerson = new Person("Petr","efimenko",22,"male",0,false); var childPerson1 = new Person("pasha","efimenko",10,"male",1231,false); childPerson1.setFather(fatherPerson); console.log(childPerson1.getFatherName()); var childPerson2 = new Person("Anya","efimenko",10,"male",1231,false); console.log(fatherPerson.addChild(childPerson1)); console.log(fatherPerson.addChild(childPerson2)); ``` ## Замыкания ###### (в папке js07) ## Рекурсия ### Sum ```javascript var n = 10; var d = 5; var a1 = 1; function sum(n, d, a1){ if(n === 0) return 0; return a1 + d * (n - 1) + sum(n - 1, d, a1); } var resSum = sum(n, d, a1); console.log(resSum); ``` ### HTML Tree ```javascript var someTree = { tagName: "table", //html tag subTags: [ //вложенные тэги { tagName: "tr", subTags: [ { tagName: "td", text: "some text", }, { tagName: "td", text: "some text 2", } ] } ], attrs: { border: 1, }, } toHTML(someTree); function toHTML(obj){ var tag = document.createElement(obj.tagName); if("attrs" in obj){ for (var keys in obj.attrs) { tag.setAttribute(keys, obj.attrs[keys]); } } if("text" in obj) tag.innerHTML = obj.text; if("subTags" in obj){ for (var i = 0; i < obj.subTags.length; i++){ var sub = toHTML(obj.subTags[i]); tag.appendChild(sub); } } if(obj.tagName === "table") document.body.appendChild(tag); else return tag; } ```