DongThin / my-love-e2e-testing

e2e testing with cypress
2 stars 0 forks source link

Apply ES6 #19

Open alang-dev opened 6 months ago

alang-dev commented 6 months ago

Essential ES6 features with examples for each:

1. Variable Declarations (let and const):

// Old way with var (can lead to scope issues)
if (true) {
  var age = 30;
}
console.log(age); // Outputs 30, even though age was declared inside the if block

// New way with let (block-scoped)
if (true) {
  let age = 30;
}
console.log(age); // ReferenceError: age is not defined (because age is not accessible outside the if block)

// Using const for constants
const PI = 3.14159;
PI = 10; // This will cause a TypeError because PI is constant

2. Arrow Functions:

// Traditional function
function greet(name) {
  return "Hello, " + name;
}

// Arrow function (equivalent to the above)
const greet = (name) => "Hello, " + name;

console.log(greet("Alice")); // Outputs "Hello, Alice"

3. Template Literals:

// Old way (string concatenation)
const name = "Bob";
const greeting = "Hello, " + name + "!";

// Template literal (more readable)
const greeting = `Hello, ${name}!`;

console.log(greeting); // Outputs "Hello, Bob!"

4. Destructuring Assignment:

// Old way
const numbers = [10, 20, 30];
const firstNumber = numbers[0];
const secondNumber = numbers[1];

// Destructuring assignment (cleaner)
const [firstNumber, secondNumber] = numbers;

console.log(firstNumber, secondNumber); // Outputs 10 20

5. Spread Operator (...):

// Combining arrays
const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];
const allNumbers = [...numbers1, ...numbers2];

console.log(allNumbers); // Outputs [1, 2, 3, 4, 5, 6]

// Spreading object properties
const person = { name: "Alice" };
const details = { age: 30, city: "New York" };
const combined = { ...person, ...details };

console.log(combined); // Outputs { name: "Alice", age: 30, city: "New York" }

6. Default Parameters:

function greet(name = "World") { // "World" is the default value
  return "Hello, " + name;
}

console.log(greet());    // Outputs "Hello, World" (default name used)
console.log(greet("Bob")); // Outputs "Hello, Bob"

7. Rest Parameters (...):

function sum(...numbers) { // ...numbers collects remaining arguments into an array
  let total = 0;
  for (const num of numbers) {
    total += num;
  }
  return total;
}

console.log(sum(1, 2, 3));     // Outputs 6
console.log(sum(10, 20, 30, 40)); // Outputs 100

8. Classes:

class Car {
  constructor(model, year) {
    this.model = model;
    this.year = year;
  }

  getInfo() {
    return `This is a ${this.year} ${this.model} car.`;
  }
}

const myCar = new Car("Tesla Model S", 2024);
console.log(myCar.getInfo()); // Outputs "This is a 2024 Tesla Model S car."

9. Modules:

10. Promises:

fetch('https://api.example.com/data')
  .then(response => response.json()) // Parse the JSON response
  .then(data => console.log(data)) // Process the data
  .catch(error => console.error(error)); // Handle errors

11. Iterators and for...of Loops:

const numbers = [1, 2, 3, 4, 5];

// Using for...of loop
for (const number of numbers) {
  console.log(number);
}

// Using iterator (less common, but more versatile)
const numbersIterator = numbers[Symbol.iterator]();
let next = numbersIterator.next();
while (!next.done) {
  console.log(next.value);
  next = numbersIterator.next();
}

12. Enhanced Object Literals:

const name = "Alice";
const age = 30;

// Old way
const person = {
  name: name,
  age: age
};

// New way (shorthand properties)
const person = { name, age };

// Computed property names
const key = "city";
const city = "New York";
const personWithCity = { [key]: city }; // personWithCity = { city: "New York" }

// Method definitions
const personWithGreet = {
  name: "Bob",
  greet() {
    return "Hello, " + this.name;
  }
};

console.log(personWithGreet.greet()); // Outputs "Hello, Bob"

13. String and Array Methods:

alang-dev commented 6 months ago
alang-dev commented 5 months ago