-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewInjavaScript.js
More file actions
86 lines (65 loc) · 1.85 KB
/
Copy pathnewInjavaScript.js
File metadata and controls
86 lines (65 loc) · 1.85 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
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
var getGlobal = function() {
// the only reliable means to get the global object was this, you no longer need to do this
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
};
//you can simply use this
const theGlobalThis = globalThis;
class Point {
#x; //private
#y; //private
constructor(x, y) {
this.#x = x;
this.#y = y;
}
equals(point) {
return this.#x === point.#x && this.#y === point.#y;
}
}
class Animal {
constructor(name) {
this.name = name;
}
}
class Cat extends Animal {
constructor(name) {
super(name);
this.likesBaths = false;
}
mewo() {
console.log("mewo");
}
}
class Cat2 extends Animal {
//no need to call the constructor of the super class
#
likesBaths = false;
mewo() {
console.log("mewo");
}
}
//Numeric separators for easier reading code
const million = 1 _000_000;
const billion = 1 _000_000_000;
const object = { x: 42, y: 50 };
const entries = Object.entries(object);
//now we can get the previous object using this
const result = Object.fromEntries(entries);
//but we can filter and map a new object using.filter and .map and return a new object
const obj = { abc: 1, def: 2, ghij: 3 };
const res = Object.fromEntries(
Object.entries(obj)
.filter(([key, val]) => key.length === 3)
.map(([key, val]) => [key, val * 2])
);
// res is { 'abc': 2, 'def': 4 }
var arr1 = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]
//Or you can Use arr2.flat(Infinity);
//[1,2,3,4,5,6]