-
Notifications
You must be signed in to change notification settings - Fork 21
/
oop.js
54 lines (48 loc) · 1.13 KB
/
oop.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
// Factory Functions
// function createElf(name, weapon) {
// return {
// name,
// weapon,
// attack() {
// console.log(`${this.name} attacks with ${this.weapon}`);
// },
// };
// }
// Object.create
// const elfFunction = {
// attack() {
// console.log(`${this.name} attacks with ${this.weapon}`);
// },
// };
// function createElf(name, weapon) {
// const newElf = Object.create(elfFunction);
// newElf.name = name;
// newElf.weapon = weapon;
// return newElf;
// }
// const sam = createElf("Sam", "stones");
// const peter = createElf("Peter", "fire");
// sam.attack();
// peter.attack();
// Constructor Functions
// function Elf(name, weapon) {
// this.name = name;
// this.weapon = weapon;
// this.attack = function () {
// console.log(`${this.name} attacks with ${this.weapon}`);
// };
// }
// ES6 Classes
class Elf {
constructor(name, weapon) {
this.name = name;
this.weapon = weapon;
}
attack() {
console.log(`${this.name} attacks with ${this.weapon}`);
}
}
const sam = new Elf("Sam", "stones");
const peter = new Elf("Peter", "fire");
sam.attack();
peter.attack();