-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreational_patterns.js
65 lines (54 loc) · 1.55 KB
/
creational_patterns.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
var PeopleFactory = function(name, age, state) {
var temp = {};
temp.age = age;
temp.name = name;
temp.state = state;
temp.printPerson = function() {
console.log(`${this.name}:${this.age}:${this.state}`);
}
return temp;
};
/* let me = new PeopleFactory('sigfried', 28, 'CF');
let pandushka = new PeopleFactory('pandushka', 32, 'STP')
me.printPerson();
pandushka.printPerson(); */
var PeopleConstructor = function(name, age, state) {
this.name = name;
this.age = age;
this.state = state;
this.printPerson = function () {
console.log(`${this.name}: ${this.age}: ${this.state}`);
}
};
/* var me = new PeopleConstructor('sigfried', 28, 'CF');
var eva = new PeopleConstructor('pandushka', 32, 'STP');
me.printPerson();
eva.printPerson(); */
var PeopleProto = function() {
};
PeopleProto.prototype.age = 0;
PeopleProto.prototype.name = 'no name';
PeopleProto.prototype.state = 'No city';
PeopleProto.prototype.printPerson = function () {
console.log(`${this.name}:${this.age}:${this.state}`);
};
/*
var me = new PeopleProto();
me.name = 'sigfried';
me.age = 28;
me.state = 'CF';
console.log('name' in me);
console.log(me.hasOwnProperty('name'));
me.printPerson(); */
var PeopleDynamicProto = function(name, age, state) {
this.age = age;
this.name = name;
this.state = state;
if (typeof this.printPerson !== 'function') {
PeopleDynamicProto.prototype.printPerson = function () {
console.log(`${this.name}:${this.age}:${this.state}`);
};
}
};
var me = new PeopleDynamicProto('sigfried', 28, 'CF');
me.printPerson();