-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbs1.js
99 lines (93 loc) · 2.75 KB
/
bs1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//git task
//realization with constructors function
var Animal = function(age,name,sound,region){
this.age = age;
this.name = name;
this.sound = sound;
this.region = region;
this.say = function(){
return "I'm " + this.name + " and i say " + this.sound
};
};
//goAway functions
var Dog = new Animal(1,"Bob","gau","nerby");
var Cat = new Animal(1,"James","meow","far");
var Woodpecker = new Animal(1,"Sunny","?","far away");
Dog.goAway = function(sentence){
return Dog.name + ", " + sentence;
}
Cat.goAway = function(sentence){
return Cat.name + ", " + sentence;
}
Woodpecker.goAway = function(sentence){
return Woodpecker.name + ", " + sentence;
}
//getType with this and with obj
var getType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); //Ready solution for getType that i find
};
//own solution
var getTypeOwn = function(){
if (typeof this.name === "string" && typeof this.say === "function" && typeof this.sound === "string"){
return "Animal";
}
else{
return "Not Animal";
};
};
//call objects method
console.log(Dog.say());
console.log(Cat.say());
console.log(Woodpecker.say());
console.log(getTypeOwn.call(Dog));
console.log(getTypeOwn.call(Cat));
console.log(getTypeOwn.call(Woodpecker));
console.log(Dog.goAway("what are you doing?"));
console.log(Cat.goAway("what are you doing?"));
console.log(Woodpecker.goAway("what are you doing?"))
//realization with Object.create()
console.log("Another method \n");
var Animalcreate = {
constructor:function(age,name,sound,region){
this.age = age;
this.name = name;
this.sound = sound;
this.region = region;
this.say = function(){
return "I'm " + this.name + " and i say " + this.sound
};
return this;
}
}
//create Dog,Cat,woodpecker
var dogObject = Object.create(Animalcreate).constructor(2,"Boby","gauh","nerby");
var catObject = Object.create(Animalcreate).constructor(1,"Carl","meow","nerby");
var woodpeckerObject = Object.create(Animalcreate).constructor(1,"Susy","?","far away");
//define goAway and getType
dogObject.goAway=function(){
return "Oh, no !"
}
catObject.goAway=function(){
return "What?"
}
woodpeckerObject.goAway=function(){
return "OH NO!"
}
var getTypeOwn = function(){
if (typeof this.name === "string" && typeof this.say === "function" && typeof this.sound === "string"){
return "Animal";
}
else{
return "Not Animal";
};
};
//call methods of objects
console.log(dogObject.say());
console.log(catObject.say());
console.log(woodpeckerObject.say());
console.log(dogObject.goAway());
console.log(catObject.goAway());
console.log(woodpeckerObject.goAway());
console.log(getTypeOwn.call(dogObject));
console.log(getTypeOwn.call(catObject));
console.log(getTypeOwn.call(woodpeckerObject));