-
Notifications
You must be signed in to change notification settings - Fork 129
/
Classes.js
executable file
·144 lines (121 loc) · 3.37 KB
/
Classes.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Classes
*
*/
// Classes in ES6 are just syntactical sugar over JavaScript's
// existing prototype-based inheritance
// Simple, clean syntax to create objects and take care of inheritance
// Pre-ES6 way:
// function Person(name, age) {
// this.name = name;
// this.age = age;
// }
// Person.prototype.greetings = function() {
// console.log("Greetings :D");
// }
// function Employee(name, age, role) {
// Person.call(this, name, age);
// this.role = role;
// }
// Employee.prototype = Object.create(Person.prototype);
// Employee.prototype.constructor = Employee;
// const bill = new Employee("Bill", 41, "janitor");
// bill.greetings();
// Classes - a sort of special function
// Classes are NOT hoisted
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greetings() {
return "classes r the kewlest :D";
}
}
// Super keyword calls functions on an object's parent class
class Employee extends Person {
constructor(name, age, position) {
super(name, age);
this.position = position;
}
sayGreeting() {
const parentString = super.greetings();
console.log(`${this.name} thinks ${parentString}`)
}
}
class Customer extends Person {
constructor({name="customer", age="n/a", contactMethod}) {
super(name, age);
this.contactMethod = contactMethod;
this.accountCredit = null;
}
addCredit(amount) {
this.accountCredit += amount;
}
reduceCredit(amount) {
this.accountCredit -= amount;
}
static sayCustomerNames(...customers) {
for(const c of customers){
console.log(c.name);
}
}
static transferCredit(source, target) {
const amt = source.accountCredit;
target.accountCredit += amt;
source.accountCredit -= amt;
}
}
const customer1 = new Customer({name: "Bob", contactMethod: "email"});
const customer2 = new Customer({name: "Jane Doe", contactMethod:"mobile"});
const customer3 = new Customer({name: "Ashley", contactMethod: "email"});
customer1.addCredit(100);
customer1.reduceCredit(50);
customer2.addCredit(25);
// console.log(customer1.accountCredit);
// Customer.transferCredit(customer2, customer1);
// console.log(customer1.accountCredit, customer2.accountCredit);
class FamilyMember {
constructor(lastName, firstName, relationship) {
this.lastName = lastName;
this.firstName = firstName;
this.relationship = relationship;
}
sayFamilyName() {
console.log(`We are the ${this.lastName}s`)
}
}
class FamilyGroup {
constructor(parents=[],children=[]) {
this.parents = parents;
this.children = children;
}
addMember(member) {
this.children.push(member);
}
}
const smithFamily = {
1: ["Smith", "Bill", "father"],
2: ["Smith", "Catherine", "mother"],
3: ["Smith", "Annie", "daughter"],
4: ["Smith", "Will", "son"],
}
const anotherFamily = {
1: ["Wilson", "BillyBob", "father"],
2: ["Wilson", "JoeyJoeJoe", "son"]
}
const createFamily = (famObj) => {
const newFamGroup = new FamilyGroup();
for(const prop in famObj) {
const [last, first, relationship] = famObj[prop];
const newMember = new FamilyMember(last, first, relationship);
if (relationship === "father" || relationship === "mother") {
newFamGroup.parents.push(newMember);
} else {
newFamGroup.children.push(newMember);
}
}
return newFamGroup;
}
const theSmiths = createFamily(smithFamily);
const theWilsons = createFamily(anotherFamily);