forked from sumitkumar2019/CompleteJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle.js
More file actions
44 lines (40 loc) · 1.41 KB
/
circle.js
File metadata and controls
44 lines (40 loc) · 1.41 KB
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
//ES6 Modules : used with browser : by default everything in this file is private
//Implementation details: hidden: private properties
const _radius = new WeakMap();
const _move = new WeakMap();
const privateProps = new WeakMap();
//Public Interface: exposed
export class Circle{
constructor(radius){
privateProps.set(this, { //not recommendable but can do like this(better will be seperate each properties as a weak map)
radius:radius,
move:()=>console.log(this, 'move')
});
_radius.set(this, radius);
_move.set(this, /**function(){
console.log('move', this); //here 'this' will undefined because body of the class execute in strict mode
} */
()=>console.log('move', this) // if we use arrow function then 'this' will be the Circle object
);
}
draw(){
console.log(_radius.get(this));
}
move(){
_move.get(this)();
}
//Getter : Poor approach : for better approach
getRadius(){ // in this getter approach we need to call getRadius() method every time
return _radius.get(this);
}
/**Getters and Setters */
//Getter: better approach : ES6 classes
get radius() {
return _radius.get(this);
}
//Setter: better approach : ES6 classes
set radius(radius) {
if(radius<=0) throw new Error("invalid radius");
_radius.set(this, radius);
}
}