summaryrefslogtreecommitdiff
path: root/t.js
blob: 0b1995727120d70157f27dd792a3cff92498856f (plain)
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

const personFactory = (setup) => {
    
    let person = {

        who: {
            // name: '',
            // surname: '',
            // age: 0,
            // job: 'unpemployed'
        },

        init: function (json) {
            // this.who.name = json.name;
            // this.who.surname = json.surname;
            // this.who.job = json.job;
            // this.who.age = json.age;
            this.who = json;
            return this;
        },

        whoami: function() {
            let who = this.who;
            return `${who.name} ${who.surname}, ${who.job}, ${who.age} years old`;
        },

        setJob: function (job) {
            this.who.job = job;
            return this;
        },

        grow: function(years) {
            if (years == null) this.who.age++;
            else this.who.age += years;
            return this;
        }
        
    }
    
    return person.init(setup)
    
}

geo = personFactory({
    name: 'George', surname: 'Xalkis', age: 56
})
console.log('@@init', geo.whoami());

geo.setJob('developer').grow(4);
console.log(geo.whoami());