|
| 1 | +/* 1. ES6 Destructuring |
| 2 | +Problem: You have an object representing a person. Use ES6 destructuring to extract the name and age properties. */ |
| 3 | + |
| 4 | +let person = { |
| 5 | + usernName: "mdSaifulIslam", |
| 6 | + age: 30, |
| 7 | +}; |
| 8 | +const { userName, age } = person; |
| 9 | +console.log(`I am ${userName} and I am ${age}`); //output "I am undefined and I am 30" |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +/* 2. ES6 Module |
| 15 | +Problem: Create a module that exports a function to calculate the area of a circle and import it into another file. */ |
| 16 | +// got to index.html , import.js and module.js file |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +/* 3. ES6 Classes |
| 25 | +Problem: Create a class Rectangle with properties width and height and a method to calculate the area. */ |
| 26 | + |
| 27 | +class recTangle { |
| 28 | + constructor(width, height) { |
| 29 | + this.width = width; |
| 30 | + this.height = height; |
| 31 | + } |
| 32 | + area() { |
| 33 | + return this.width * this.height; |
| 34 | + } |
| 35 | +} |
| 36 | +let rectangleOne = new recTangle(150, 80); |
| 37 | +console.log(rectangleOne.area()); //output is 12000 |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +/* 4. Getter and Setter in ES6 |
| 45 | +Problem: Create a class Person with a getter and setter for the fullName property */ |
| 46 | + |
| 47 | +let personBox = { |
| 48 | + fristName: "md saiful", |
| 49 | + lastName: "islam", |
| 50 | + age: 20, |
| 51 | + get FullName() { |
| 52 | + return this.fristName + " " + this.lastName; // output md Ridoy Ahmed |
| 53 | + }, |
| 54 | + set myFristName(editFristName) { |
| 55 | + this.fristName = editFristName; |
| 56 | + }, |
| 57 | + set myLastName(editLastName) { |
| 58 | + this.lastName = editLastName; |
| 59 | + }, |
| 60 | +}; |
| 61 | + |
| 62 | +// edit fristName; |
| 63 | +personBox.myFristName = "md Ridoy"; |
| 64 | +//eidt lastName: |
| 65 | +personBox.myLastName = "Ahmed"; |
| 66 | + |
| 67 | +// getter output fullname |
| 68 | +console.log(personBox.FullName); // output md Ridoy Ahmed |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +/* 5. Class Expressions |
| 77 | +Problem: Create a class expression for a Circle with a method to calculate the circumference.*/ |
| 78 | + |
| 79 | +class cirCale { |
| 80 | + constructor(radius) { |
| 81 | + this.radius = radius; |
| 82 | + } |
| 83 | + circlaeCalculate() { |
| 84 | + return 2 * Math.PI * this.radius; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +let circaleOne = new cirCale(5); |
| 89 | + |
| 90 | +console.log(circaleOne.circlaeCalculate());// 31.41592653589793 |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +/* 6. JavaScript Computed Properties |
| 99 | +Problem: Create an object with computed property names based on variables.*/ |
| 100 | + |
| 101 | +let myNameIs = "md"; |
| 102 | + |
| 103 | +let perSon = { |
| 104 | + [myNameIs]: "MD SAIFUL ISLAM", |
| 105 | + |
| 106 | +}; |
| 107 | + |
| 108 | +console.log(perSon[myNameIs]); //OutPut MD SAIFUL ISLAM |
| 109 | +// or |
| 110 | +console.log(perSon["md"]); //OutPut MD SAIFUL ISLAM |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +/* 7. Inheritance |
| 119 | +Problem: Create a base class Animal and a derived class Dog that inherits from Animal. */ |
| 120 | + |
| 121 | +class Animal { |
| 122 | + constructor(dogName) { |
| 123 | + this.dogName = dogName; |
| 124 | + } |
| 125 | + myDog() { |
| 126 | + return `my dog name si ${this.dogName}`; |
| 127 | + } |
| 128 | +} |
| 129 | +class dog extends Animal { |
| 130 | + constructor(dogName, dogAge) { |
| 131 | + super(dogName); |
| 132 | + this.dogAge = dogAge; |
| 133 | + } |
| 134 | + myDogIfno() { |
| 135 | + return `${this.myDog()} and he ${this.dogAge} years old`; |
| 136 | + } |
| 137 | +} |
| 138 | +let Dog1 = new dog("Chappie", 9); |
| 139 | +console.log(Dog1.myDogIfno()); //output my dog name si Chappie and he 9 years old |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +/*8. New Target Operators |
| 150 | +Problem: Use new.target to create an abstract class Shape that cannot be instantiated directly.*/ |
| 151 | + |
| 152 | +function shapeBox(shapeNew) { |
| 153 | + if (!new.target) { |
| 154 | + throw "no data available "; |
| 155 | + } |
| 156 | + this.shapeNew = shapeNew; |
| 157 | +} |
| 158 | +let sahpeOne = new shapeBox("rectangle"); |
| 159 | +console.log(sahpeOne); |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +// or another way |
| 164 | + |
| 165 | +let shapeBoxData = class { |
| 166 | + constructor(shape) { |
| 167 | + if (!new.target) { |
| 168 | + throw "Shape that cannot be instantiated directly."; |
| 169 | + } |
| 170 | + this.shape = shape; |
| 171 | + } |
| 172 | +}; |
| 173 | + |
| 174 | +let recTanGle = class extends shapeBoxData { |
| 175 | + constructor(shape) { |
| 176 | + super(shape); |
| 177 | + } |
| 178 | +}; |
| 179 | + |
| 180 | +let newShapeOne = new recTanGle("rectangle"); |
| 181 | +console.log(newShapeOne); // outPut recTanGle { shape: 'rectangle' } |
| 182 | + |
| 183 | +console.log(newShapeOne.shape); //outPut rectangle |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | +/* |
| 193 | +9. Static Methods |
| 194 | +Problem: Create a class MathUtil with a static method square to calculate the square of a number. */ |
| 195 | + |
| 196 | +let MathUtil = class { |
| 197 | + constructor(number) { |
| 198 | + this.number = number; |
| 199 | + } |
| 200 | + |
| 201 | + get num() { |
| 202 | + return this.number; |
| 203 | + } |
| 204 | + |
| 205 | + //static mathod |
| 206 | + static square() { |
| 207 | + return Math.sqrt(625); |
| 208 | + } |
| 209 | +}; |
| 210 | + |
| 211 | +let numberOne = new MathUtil(625); |
| 212 | +let numberTwo = new MathUtil(2000); |
| 213 | + |
| 214 | +//static method output is { 25} |
| 215 | +console.log(MathUtil.square()); |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | +/* 10. Symbol |
| 224 | +Problem: Create an object with a symbol as a property key and demonstrate accessing it.*/ |
| 225 | + |
| 226 | +let nameIs = Symbol.for("this code is runing now"); |
| 227 | + |
| 228 | +let dataObject = { |
| 229 | + [nameIs]: "sakib", |
| 230 | + roll: 252, |
| 231 | + age: 20, |
| 232 | +}; |
| 233 | + |
| 234 | +//total ouput |
| 235 | +console.log(dataObject); |
| 236 | + |
| 237 | +//symbol as a property key |
| 238 | +console.log(Object.keys(dataObject)); |
0 commit comments