|
@@ -1,4 +1,4 @@
|
|
|
-##Initialize
|
|
|
+## Initialize
|
|
|
```javascript
|
|
|
function Person(name, surname, age, sex, salary, married){
|
|
|
this.name = name,
|
|
@@ -11,101 +11,177 @@ function Person(name, surname, age, sex, salary, married){
|
|
|
|
|
|
var person = new Person("Ivan", "Petroff")
|
|
|
```
|
|
|
-##
|
|
|
+## getFatherName
|
|
|
```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
|
|
|
+this.name = name;
|
|
|
+this.surname = surname;
|
|
|
+this.age = age;
|
|
|
+this.sex = sex;
|
|
|
+this.salary = salary;
|
|
|
+this.married = married;
|
|
|
+var childs = [];
|
|
|
+
|
|
|
|
|
|
- var age = 0
|
|
|
+ this.getName = function(){
|
|
|
+ return this.name;
|
|
|
+ }
|
|
|
+ this.setName = function(newName){
|
|
|
+ if (typeof(newName)== "string"){
|
|
|
+ this.name = newName;
|
|
|
+ }
|
|
|
+ return this.name;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ this.getSurname = function(){
|
|
|
+ return this.surname;
|
|
|
+ }
|
|
|
+ this.setSurname = function(newSurname){
|
|
|
+ if (typeof(newName)== "string"){
|
|
|
+ this.surname = newSurname;
|
|
|
+ }
|
|
|
+ return this.surname;
|
|
|
+ }
|
|
|
|
|
|
+
|
|
|
+ this.getAge = function(){
|
|
|
+ return this.age;
|
|
|
+ }
|
|
|
this.setAge = function(newAge){
|
|
|
- newAge = +newAge
|
|
|
- if (!isNaN(newAge) && Number.isInteger(newAge) && (newAge > 0) && (newAge < 100)){
|
|
|
- age = newAge
|
|
|
+ if (!isNaN(newAge) && Number.isInteger(newAge) && (newAge > 0) && (newAge < 120)){
|
|
|
+ this.age = newAge;
|
|
|
}
|
|
|
+ return this.age;
|
|
|
+ }
|
|
|
|
|
|
- return age
|
|
|
+
|
|
|
+ this.getSex = function(){
|
|
|
+ return this.sex;
|
|
|
}
|
|
|
-
|
|
|
- this.setSex = function(newSex){
|
|
|
- newSex!='male'? 'female' : 'male'
|
|
|
- {sex=newSex}
|
|
|
- return sex
|
|
|
- }
|
|
|
-
|
|
|
- this.setName = function(newName){
|
|
|
- if (newName!="" && newName!=false){
|
|
|
- name=newName
|
|
|
- }
|
|
|
- return name
|
|
|
- }
|
|
|
-
|
|
|
- this.setSurname = function(newSurname){
|
|
|
- if (newSurname!="" && newSurname!=false){
|
|
|
- surname=newSurname
|
|
|
- }
|
|
|
- return surname
|
|
|
- }
|
|
|
- var salary=0
|
|
|
- this.setSalary = function(newSalary){
|
|
|
- newSalary = +newSalary
|
|
|
- if (!isNaN(newSalary) && Number.isInteger(newSalary) && (newSalary > 0) && (newSalary < 10000)){
|
|
|
- salary = newSalary
|
|
|
+ this.setSex = function(newSex){
|
|
|
+ if (newSex.toLowerCase() == "male" || "female"){
|
|
|
+ this.sex = newSex;
|
|
|
}
|
|
|
- return salary
|
|
|
- }
|
|
|
-
|
|
|
- this.setMarried = function(newMarried){
|
|
|
- if(newMarried!=true){
|
|
|
- married=newMarried
|
|
|
- }
|
|
|
- return married
|
|
|
- }
|
|
|
-
|
|
|
- this.getMarried = function(){
|
|
|
- return married
|
|
|
+ return this.sex;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ this.getSalary = function(){
|
|
|
+ return this.salary;
|
|
|
}
|
|
|
-
|
|
|
- this.getSalary = function(){
|
|
|
- return salary
|
|
|
+ this.setSalary = function(newSalary){
|
|
|
+ if (!isNaN(parseFloat(newSalary))){
|
|
|
+ this.salary = newSalary;
|
|
|
}
|
|
|
-
|
|
|
- this.getSurname = function(){
|
|
|
- return surname
|
|
|
+ return this.salary;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ this.getMarried = function(){
|
|
|
+ return this.married;
|
|
|
}
|
|
|
-
|
|
|
- this.getName = function(){
|
|
|
- return name
|
|
|
+ this.setMarried = function(newMarried){
|
|
|
+ this.married = newMerried;
|
|
|
+ return this.married;
|
|
|
}
|
|
|
- this.getAge = function(){
|
|
|
- return age
|
|
|
+
|
|
|
+
|
|
|
+ this.setFather = function(father){
|
|
|
+ this.father = father;
|
|
|
+ return this.father;
|
|
|
}
|
|
|
- this.getSex = function(){
|
|
|
- return sex
|
|
|
+
|
|
|
+ this.getFatherName = function(){
|
|
|
+ if(this.sex == "male" && (this.father != undefined)){
|
|
|
+ return (this.father.name + "ович");
|
|
|
+ }
|
|
|
+ else if(this.sex == "female" && (this.father != undefined)){
|
|
|
+ return (this.father.name + "овна");
|
|
|
+ }
|
|
|
+ console.log("Sorry, imposible to collect fathername, can you test sex and select Father")
|
|
|
}
|
|
|
+
|
|
|
+ this.addChild = function(child){
|
|
|
+ if((this.age - child.age) > 15){
|
|
|
+ childs.push(child);
|
|
|
+ }
|
|
|
+ return childs.length;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
-var person = new Person("Ivan", "Petroff")
|
|
|
+var father = new Person("Ivan", "Petrov", 50, "male", 100500, true)
|
|
|
+var daughter = new Person("Maria", "Petrova", 25, "female", 500, false)
|
|
|
+
|
|
|
+
|
|
|
+var father2 = new Person("iPhone", "Sedmoy", 45, "male", 500, true)
|
|
|
+var son = new Person("Ivan", "Sedmoy", 20, "male", 200, false)
|
|
|
+
|
|
|
+daughter.setFather(father)
|
|
|
+daughter.getFatherName() // => "Ivanovna"
|
|
|
+
|
|
|
+son.setFather(father2)
|
|
|
+son.getFatherName() // => "iPhoneovich"
|
|
|
+
|
|
|
+father.addChild(daughter, daughter)
|
|
|
```
|
|
|
-##
|
|
|
+## Рекурсия
|
|
|
+
|
|
|
+## Sum
|
|
|
```javascript
|
|
|
+function sum(n){
|
|
|
+var sums = 0;
|
|
|
+ if(n < 1){
|
|
|
+ return sums;
|
|
|
+ } return sums += n + sum(n-1);
|
|
|
+}
|
|
|
|
|
|
+console.log(sum(100))
|
|
|
```
|
|
|
-##
|
|
|
+## makeCounter
|
|
|
```javascript
|
|
|
+function makeCounter(){
|
|
|
+ var counter = 0;
|
|
|
+ return function(){
|
|
|
+ return counter++
|
|
|
+ }
|
|
|
+}
|
|
|
+var counter = makeCounter()
|
|
|
+var otherCntr = makeCounter()
|
|
|
+
|
|
|
+ counter() // 1
|
|
|
+ counter() // 2
|
|
|
+
|
|
|
+ otherCntr() // 1
|
|
|
+ otherCntr() // 2
|
|
|
|
|
|
```
|
|
|
-##
|
|
|
+## makeProfileTimer
|
|
|
```javascript
|
|
|
+function makeProfileTimer(){
|
|
|
+ var t = performance.now();
|
|
|
+ return function(){
|
|
|
+ var t2 = performance.now();
|
|
|
+ return (t2 - t);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+var timer = makeProfileTimer();
|
|
|
+
|
|
|
+
|
|
|
+alert(timer());
|
|
|
|
|
|
```
|
|
|
-##
|
|
|
+## makeSaver
|
|
|
```javascript
|
|
|
+var saver = makeSaver(Math.random)
|
|
|
+function makeSaver(someFunct){
|
|
|
+ var result = 0;
|
|
|
+ return saver = function(){
|
|
|
+ return (result === 0) ? result = someFunct() : result;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
```
|