-
Notifications
You must be signed in to change notification settings - Fork 2
/
es6.js
286 lines (246 loc) · 5.3 KB
/
es6.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//'use strict'
let textData = [
[
"Hello",
"HI"
],
[
"Hello",
"How"
],
[
"Whats",
"up"
]
]
/*textData.map(m=>{
m.forEach(innerM=>{
console.log(innerM)
})
})
*/
textData = textData.flatMap(m=>{return m})
textData.forEach(m=>console.log(m))
let nums = [35,4,5,8]
console.log('Slice')
let newNums = nums.slice(0,2)
console.log(newNums)
console.log('Splice')
newNums = nums.splice(0,2,[16,6,8,9])
console.log(nums.flatMap(n=>{return n}))
const usrObj = {
id: 0,
name: 'Xyz',
address: {
street: 'abc'
}
}
console.log(Object.keys(usrObj))
console.log(Object.values(usrObj))
let usrs=[
{
name: 'Abc',
city: 'Blr'
},{
name : 'Xyz',
city: 'Del'
},
{
name : 'Cdf',
city: 'Del'
}
]
let groupByCity = usrs.reduceRight((accumulator,user)=>{
let key = user['city']
if(!accumulator[key]){
accumulator[key]=[];
}
accumulator[key].push(user)
return accumulator
},{})
console.log(groupByCity)
const evenNos = [2,4,6,8,10]
let result = evenNos.reduce((accumulator,num)=>{
return accumulator + num
})
console.log(`Sum is : ${result}`)
const welcomeMsgs = ['Hello','Hi','Hey']
let welcomeMsgsAcc = welcomeMsgs.reduceRight((acc,m)=>{
return acc + m
})
console.log(welcomeMsgsAcc)
let emps = [
{
id: 1,
name: 'John',
designation: 'Programmer'
},
{
id: 2,
name: 'Bob',
designation: 'Programmer'
},
{
id: 3,
name: 'Adams',
designation: 'Developer'
},
{
id: 4,
name: 'Ram',
designation: 'Manager'
}
]
let groupedEmps = emps.reduce((acc,emp)=>{
let groupBy = emp['designation']
if(!acc[groupBy]){
acc[groupBy] = []
}
acc[groupBy].push(emp)
return acc
},{})
console.log(groupedEmps)
let uUser = {
uId: 1,
uName: 'Mohsin'
}
Object.create(uUser)
console.log(uUser)
var uUsrs =
{
email :'[email protected]'
}
const render = ()=>{
console.log('Render ',this.uUsrs.email)
}
render()
let oddNums = [1,3,5,7]
oddNums= oddNums.fill([11,13],0,2).flatMap(n=>{return n})
console.log(oddNums)
// classes / objects
class A{
id
name
constructor(_id,_name){
this.id = _id
this.name = _name
}
sayHello(){
console.log(`sayHello() ${this.name}`)
this.name =this.name + ' World'
}
getHello(){
return 'getHello() '.concat(this.name);
}
static add(i,j){
return this.compute(i,j)
}
static compute(i,j){
return i * j
}
/*sorted(aa){
return sorted array
}*/
}
class B extends A{
constructor(name){
// call super class constructor
// decide whether to pass variables or not
super()
// make this available for super class
this.name = name
}
getHello(){
console.log(`B says Hello & A says ${super.getHello()}`)
}
}
console.log(A.add(12,2))
// create instance
let aObj = new A(1,'Hello how r u')
aObj.sayHello()
let _msg = aObj.getHello()
console.log(_msg)
let bObj = new B('Hello')
bObj.getHello()
console.log('Test ')
// ECMA Class declaration
const Employee = class {
firstName
lastName
constructor(fName,lName){
this.firstName = fName
this.lastName = lName
}
getEmployee(){
return { user: this.firstName+' '+this.lastName }
}
}
const empU= new Employee('John','Wick')
console.log(empU.getEmployee())
class ArithError extends Error{
constructor(name,message){
super(name,message)
}
}
// Error class
class C {
calculate(i,j){
try {
if(j==0 || j==1){
if(j==0)
throw new ArithError('Division / Zero')
else
throw new Error('Error Occured')
}
console.log(i/j)
} catch (error) {
if(error instanceof ArithError)
console.error(error)
else
console.log(error)
}
}
}
class CustomError extends Error{
constructor(name,message){
super(name,message)
}
}
//
bObj = new A(3,'A Again')
console.log(bObj.getHello())
let cObj = new C()
cObj.calculate(10,1)
//
const testAsync =async ()=>{
return fetch('https://jsonplaceholder.typicode.com/posts')
}
const callerTest =()=>{
testAsync()
.then(res => res.json())
.then((data)=>{
let posts = []
posts = data.slice(0,10)
console.log(posts)
})
.catch(err=>{
console.error(err)
})
}
callerTest()
console.log('test async() completed ')
let uDetails = function(city, state) {
return this.name + " " + this.email + "," + city + "," + state;
}
let _usr = {
name : "Mohsin",
email : "[email protected]",
}
console.log(uDetails.call(_usr, "Srinagar", "J & K"))
console.log(uDetails.apply(_usr, ["Srinagar", "J & K"]))
boundDetails = uDetails.bind(_usr)
console.log(boundDetails(["Srinagar", "J & K"]))
// functional programming
// pure functions
// high order functions
// Symbol