-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
238 lines (147 loc) · 4.28 KB
/
script.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
/* 1. ES6 Destructuring
Problem: You have an object representing a person. Use ES6 destructuring to extract the name and age properties. */
let person = {
usernName: "mdSaifulIslam",
age: 30,
};
const { userName, age } = person;
console.log(`I am ${userName} and I am ${age}`); //output "I am undefined and I am 30"
/* 2. ES6 Module
Problem: Create a module that exports a function to calculate the area of a circle and import it into another file. */
// got to index.html , import.js and module.js file
/* 3. ES6 Classes
Problem: Create a class Rectangle with properties width and height and a method to calculate the area. */
class recTangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
let rectangleOne = new recTangle(150, 80);
console.log(rectangleOne.area()); //output is 12000
/* 4. Getter and Setter in ES6
Problem: Create a class Person with a getter and setter for the fullName property */
let personBox = {
fristName: "md saiful",
lastName: "islam",
age: 20,
get FullName() {
return this.fristName + " " + this.lastName; // output md Ridoy Ahmed
},
set myFristName(editFristName) {
this.fristName = editFristName;
},
set myLastName(editLastName) {
this.lastName = editLastName;
},
};
// edit fristName;
personBox.myFristName = "md Ridoy";
//eidt lastName:
personBox.myLastName = "Ahmed";
// getter output fullname
console.log(personBox.FullName); // output md Ridoy Ahmed
/* 5. Class Expressions
Problem: Create a class expression for a Circle with a method to calculate the circumference.*/
class cirCale {
constructor(radius) {
this.radius = radius;
}
circlaeCalculate() {
return 2 * Math.PI * this.radius;
}
}
let circaleOne = new cirCale(5);
console.log(circaleOne.circlaeCalculate());// 31.41592653589793
/* 6. JavaScript Computed Properties
Problem: Create an object with computed property names based on variables.*/
let myNameIs = "md";
let perSon = {
[myNameIs]: "MD SAIFUL ISLAM",
emila: "[email protected]",
};
console.log(perSon[myNameIs]); //OutPut MD SAIFUL ISLAM
// or
console.log(perSon["md"]); //OutPut MD SAIFUL ISLAM
/* 7. Inheritance
Problem: Create a base class Animal and a derived class Dog that inherits from Animal. */
class Animal {
constructor(dogName) {
this.dogName = dogName;
}
myDog() {
return `my dog name si ${this.dogName}`;
}
}
class dog extends Animal {
constructor(dogName, dogAge) {
super(dogName);
this.dogAge = dogAge;
}
myDogIfno() {
return `${this.myDog()} and he ${this.dogAge} years old`;
}
}
let Dog1 = new dog("Chappie", 9);
console.log(Dog1.myDogIfno()); //output my dog name si Chappie and he 9 years old
/*8. New Target Operators
Problem: Use new.target to create an abstract class Shape that cannot be instantiated directly.*/
function shapeBox(shapeNew) {
if (!new.target) {
throw "no data available ";
}
this.shapeNew = shapeNew;
}
let sahpeOne = new shapeBox("rectangle");
console.log(sahpeOne);
// or another way
let shapeBoxData = class {
constructor(shape) {
if (!new.target) {
throw "Shape that cannot be instantiated directly.";
}
this.shape = shape;
}
};
let recTanGle = class extends shapeBoxData {
constructor(shape) {
super(shape);
}
};
let newShapeOne = new recTanGle("rectangle");
console.log(newShapeOne); // outPut recTanGle { shape: 'rectangle' }
console.log(newShapeOne.shape); //outPut rectangle
/*
9. Static Methods
Problem: Create a class MathUtil with a static method square to calculate the square of a number. */
let MathUtil = class {
constructor(number) {
this.number = number;
}
get num() {
return this.number;
}
//static mathod
static square() {
return Math.sqrt(625);
}
};
let numberOne = new MathUtil(625);
let numberTwo = new MathUtil(2000);
//static method output is { 25}
console.log(MathUtil.square());
/* 10. Symbol
Problem: Create an object with a symbol as a property key and demonstrate accessing it.*/
let nameIs = Symbol.for("this code is runing now");
let dataObject = {
[nameIs]: "sakib",
roll: 252,
age: 20,
};
//total ouput
console.log(dataObject);
//symbol as a property key
console.log(Object.keys(dataObject));