-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes6-classes.js
138 lines (118 loc) · 3.32 KB
/
es6-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
// function Circle(radius) {
// this.radius = radius
// this.draw = function () {
// console.log('draw')
// }
// }
// classes are essentially constructor functions
// typeof Circle returns "function"
class Circle {
//Body can contain properties
constructor(radius) {
this.radius = radius
//define a method in the constructor so it is not added to the prototype
this.move = function () {
console.log(this)
} // strict mode
}
//Defining methods
//All methods will be added to the prototype
//Instance method = available on an instance of a class (object)
draw() {
console.log('draw')
}
// Static method - not avilable on c.parse, but on the class referene (Circle.parse)
static parse(str) {
const radius = JSON.parse(str).radius
return new Circle(radius)
}
}
const c = new Circle(1)
const circle = Circle.parse('{"radius": 1}')
console.log(c)
console.log(circle)
const move = c.move
//function call -> points to global object / window in browser
move()
//Static methods = available on the class itself, not the function
//Used to create utility functions / not specific to a given object
// Hoisting = raised to the top of the code
sayHello() //works before it is initialized b/c hoisted
// sayGoodbye does not work = not hoisted
//Function Delcaration syntax - hoisted
function sayHello() {}
//Function Expression syntax
const sayGoodbye = function () {}
// Classes can be declared using declaration or expresion syntax
// Class Declaration = NOT HOISTED LIKE FUNCTION DECLARATION
class Triangle {}
// Class Expression - not used as much // more complex // less clean
const Square = class {}
//Private Properties and methods - use symbols to create private properties/methods
const _radius = Symbol() // unique identifier (will create new everytime called)
const _draw = Symbol()
class Oval {
constructor(radius) {
// Can use a symbol as a property name instead of a string
this[_radius] = radius
}
[_draw]() {
//unique identifier will be used as the name of the method
}
}
const o = new Oval(1)
const key = Object.getOwnPropertySymbols(o)[0]
console.log(o[key])
//Weakmaps for private properties/methods
const _radiuswp = new WeakMap()
const _move = new WeakMap()
class Spiral {
constructor(radius) {
_radiuswp.set(this, radius)
// use arrow method to inherit from constructor function
_move.set(this, () => {
console.log('move', this)
})
}
//to return the value of the radius property
draw() {
_move.get(this)()
console.log('draw')
}
//To get from outside
//getRadius() and call as function or to call as a property use below
get radius() {
return _radiuswp.get(this)
}
set radius(value) {
if (value <= 0) throw new Error('invalid radius')
_radiuswp.set(this, value)
}
}
const sp = new Spiral(1)
//Inheritance
class Shape {
constructor(color) {
this.color = color
}
move() {
console.log('move')
}
}
// to inherit from Shape class
class Heart extends Shape {
// if you have a constructor in the parent - must call with super() to initialize base obj
constructor(color, radius) {
super(color)
this.radius = radius
}
draw() {
console.log('draw')
}
//overriding
move() {
super.move() //to still use the parent method
console.log('circle move')
}
}
const h = new Heart('red', 1)