ECS6+ Flashcards
Como criar templates?
Utilizando `${}`. Ex: let word1 = 'Paulo'; let word2 = 'Mulotto'; const fullName = `${word1} ${word2}`
let num1 = 2; let num2 = 3; const sum= `${num1 + num2}`;
Como desestruturar um objeto?
const player = { name: 'Lebron James', club: 'LA Lakers', address: { city: 'Los Angeles' } };
// console.log( player.address.city );
const { name, club, address: { city } } = player;
// console.log(${name} plays for ${club}
);
console.log(${name} lives in ${city}
);
Objetos Literais
function addressMaker(city, state) { let newAdress = {city, state}; console.log(newAdress); // É igual a: // newAdress = {city:city, state:state};
console.log(newAdress); }
addressMaker(‘Austin’, ‘Texas’);
Criando for loops
let incomes = [62000, 67000, 75000]; let total = 0;
for (const income of incomes) {
console.log(income);
total += income;
}
let fullName = “Dylan Coding God Israel”;
for (const char of fullName) {
console.log(char);
}
// Challenge - For Of Loop
// Using the For of Loop, iterate through the array and print into the console, a message like: // Tom lives in Lisbon const students = [ { name: "John", city: "New York" }, { name: "Peter", city: "Paris"}, { name: "Kate", city: "Sidney" } ]
const students = [ { name: "John", city: "New York" }, { name: "Peter", city: "Paris"}, { name: "Kate", city: "Sidney" } ] for (const person of students) { let {name, city} = person; console.log(`${name} lives in ${city}`); }
How build a new copy of an structure like an array?
Using spread: let copy = [ …name_array ]
let contacts = [“Mary”, “Joel”, “Danny”];
let personalFriends = [ …contacts ];
contacts. push(“John”);
console. log(personalFriends);
//this also works let personalFriends = [ "David", ...contacts, "Lily" ];
It is also possible with objects:
let person = { name: "Adam", age: 25, city: "Manchester" }
let employee = { ...person, salary: 50000, position: "Software Developer" }
console.log(employee);
/* **** Challenge ****
Imagine you are going out to do some grocery shopping. So you have an array called shoppingList with all the products you want to buy. Now that you are inside of the shop, you have a basket with all the products from your list, but you want to add a few more. Create a new array called shoppingBasket, that will be a copy of the shoppingList array, and add some new products into it.
*/
const shoppingList = [“eggs”, “milk”, “butter”];
const shoppingBasket = [ …shoppingList, “bread”, “pasta”];
console.log(shoppingBasket);
Rest Operator
Irá pegar todos os argumentos (args) enviados para a função e transformar numa lista:
function add(…nums) {
console.log(nums); }
add(4, 5, 7, 8, 12)
Arrow Function
//function declaration function breakfastMenu() { return "I'm going to scrambled eggs for breakfast"; }
//anonymous function const lunchMenu = function() { return "I'm going to eat pizza for lunch"; }
// const + nome da função = (parâmetros,...) => { função } const dinnerMenu = (food) => { return `I'm going to eat a ${food} for dinner`; }
console.log( dinnerMenu(“chicken salad”) );
// para um único parâmetro: const dinnerMenu = food => `I'm going to eat a ${food} for dinner`;
console.log( dinnerMenu(“chicken salad”) );
Default values in arrow functions
const leadSinger = (artist = "someone") => { console.log(`${artist} is the lead singer of Cold Play`); }
leadSinger();
/* **** Challenge *****
Create a function that receives a parameter of food.
If your parameter food is going to have a value of “milk”
the function should print into the console the following:
“I’m going to buy milk from the grocery shop”
But if you dont pass a value to your parameter food, it should print
“I’m going to buy something from the grocery shop”
*/
const getFood = (food="something") => {return `I'm going to buy ${food} from the grocery shop `}; console.log(getFood("milk"));
includes
let numArray = [1,2,3,4,5];
console.log(numArray.includes(2))
Import & Export
// exemple.js export const data = [1,2,3];
// index.js import { data } from './example.js'; let updatedData = data;
updatedData.push(5)
console.log(updatedData);
Challenge
Inside of the file data.js, create a function add, that will receive 2 numbers and return the sum of them.
Make sure to export this function.
- Import the function add, into the index.js file
- Create a variable result, that will hold the result of the function add when you call it and pass 2 numbers into it.
- print into the console the value of the variable result;
//data.js export const add = (num1, num2) => { return num1 + num2; }
//index.js import { add } from './data.js';
let result = add(3, 2);
console.log(result);
Classes
// animal.js export class Animal { constructor(type, legs) { this.type = type; this.legs = legs; }
makeNoise(sound = 'Loud Noise') { console.log(sound); }
get metaData() { return `Type: ${this.type}, Legs: ${this.legs}`; }
static return10() { return 10; } }
export class Cat extends Animal { makeNoise(sound = "meow") { console.log(sound); } }
//index.js import { Animal, Cat } from './animal.js';
let cat = new Cat(‘Cat’, 4);
cat. makeNoise();
console. log(cat.legs);
console. log(cat.metaData);