This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict; | |
const lotteryPromise = new Promise(function (resolve, reject) { | |
console.log('lottery draw is happening 🔮'); | |
setTimeout(function () { | |
if (Math.random() >= 0.5) { | |
resolve('You WIN 💰'); | |
} else { | |
reject(new Error('You lost your money 😔')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise: An object that is used as a placeholder for the future result of an asynchronous operation. | |
⬇️ | |
Promise: A container for asynchrounusly delivered value. | |
⬇️ | |
Promise: A container for a future value. | |
👉 no call backs | |
👉 no unpredicatable results | |
👉 we can chain promises for a sequence of asynchrounous operations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Your tasks: | |
// 1. Use a constructor function to implement a 'Car'. A car has a 'make' and a | |
// 'speed' property. The 'speed' property is the current speed of the car in | |
// km/h | |
// 2. Implement an 'accelerate' method that will increase the car's speed by 10, | |
// and log the new speed to the console | |
// 3. Implement a 'brake' method that will decrease the car's speed by 5, and log | |
// the new speed to the console |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersonCl { | |
constructor(fullName, birthYear) { | |
this.fullName = fullName; | |
this.birthYear = birthYear; | |
} | |
// all functions outside the constructor() | |
// will be on the prototype | |
// not on the object itself |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Car = function(make, speed) { | |
this.make = make; | |
this.speed = speed; | |
} | |
Car.prototype.accelerate = function () { | |
console.log(`${this.make} is accelerating at ${this.speed + 10} m/hr`); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// constructor functions | |
// 1. New {} is created | |
// 2. function is called, this = {} | |
// 3. {} linked to prototype | |
// 4. function automatically return {} | |
const Person = function (firstName, birthYear) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// setTimeout | |
const ingredients = ['olives', 'anchovies']; | |
const pizzaTimer = setTimeout( | |
(ing1, ing2) => console.log(`Here is your pizza with ${ing1} and ${ing2} 🍕`), | |
3000, | |
...ingredients | |
); | |
console.log('Waiting...'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formatCur = function (value, locale, currency) { | |
return new Intl.NumberFormat(locale, { | |
style: 'currency', | |
currency: currency, | |
}).format(value); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const options2 = { | |
// style: 'unit', | |
style: 'currency', | |
// unit: 'mile-per-hour', | |
unit: 'celsius', | |
currency: 'EUR', | |
// useGrouping: false, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const calcdaysPassed = (date1, date2) => | |
Math.abs((date2 - date1) / (1000 * 60 * 60 * 24)); | |
const days1 = calcdaysPassed(new Date(2037, 3, 4), new Date(2037, 3, 14)); | |
console.log(days1); |
NewerOlder