dreamcoding
-
11. PromiseJavaScript & TypeScript 2020. 11. 15. 15:22
Promise is a JavaScript object for asynchronous operation state: pending -> fulfilled or rejected Producer vs Consumer 1. Producer when new Promise is created, the executor runs automatically const promise = new Promise((resolve, reject) => { // doing some heavy work (network, read files) console.log('doing something....'); setTimeout(() => { // resolve('ellie'); reject(new Error('no network'));..
-
10. CallbackJavaScript & TypeScript 2020. 11. 15. 14:28
JavaScript is synchronous Execute the code block in order after hoisting hoisting : var, function declaration console.log('1'); setTimeout(() => console.log('2'), 1000); console.log('3'); // 1, 3, 2 Synchronous callback function printImmediately(print) { print(); } printImmediately(() => console.log('hello')); Asynchronous callback function printWithDelay(print, timeout) { setTimeout(print, time..
-
9. JSONJavaScript & TypeScript 2020. 11. 15. 13:21
JSON (JavaScript Object Notation) simplest data interchange format lightweight text-based structure easy to read key - value pairs used for serialization and transmission of data between the network and the network connection independent programming language and platform 1. Object to JSON stringify (obj) let json = JSON.stringify(true); console.log(json); // true json = JSON.stringify(["apple", ..
-
8. Array APIsJavaScript & TypeScript 2020. 11. 14. 14:24
Quiz // Q1. make a string out of an array const fruits = ['apple', 'banana', 'orange']; const result = fruits.join(); console.log(result); // 'apple,banana,orange' // Q2. make an array out of a string const fruits = '🍎, 🥝, 🍌, 🍒'; const result = fruits.split(','); console.log(result); // ["🍎", " 🥝", " 🍌", " 🍒"] // Q3. make this array look like this: [5, 4, 3, 2, 1] const array = [1, 2, 3, 4, 5]; ..
-
7. Array, APIJavaScript & TypeScript 2020. 11. 14. 14:17
Array 1. Declaration const arr1 = new Array(); const arr2 = [1, 2]; 2. Index position const fruits = ['apple', 'banana']; console.log(fruits); // 'apple', 'banana' console.log(fruits.length); // 2 console.log(fruits[0]); // 'apple' console.log(fruits[1]); // 'banana' console.log(fruits[2]); // undefined console.log(fruits[fruits.length - 1]); // 'banana' 3. Looping over an array print all fruits..
-
5. class vs objectJavaScript & TypeScript 2020. 11. 14. 11:06
class template declare once no data in introduced in ES6 syntactical sugar over prototype-based inheritance 1. class declarations class Person { // constructor constructor (name, age) { // fields this.name = name; this.age = age; } // methods speak() { console.log(`${this.name}: hello! `); } } const ellie = new Person('ellie', 20); console.log(ellie.name); // ellie console.log(ellie.age); // 20 ..
-
4. Arrow FunctionJavaScript & TypeScript 2020. 11. 13. 18:04
Function fundamental building block in the program subprogram can be used multiple times performs a task or calculates a value 1. Function declaration function name(param1, param2) { body ... return; } one function === one thing naming: doSomething, command, verb e.g.createCardAndPoint ->. createCard, createPoint function is object in JS function printHello() { console.log('Hello'); } printHello..
-
3. operator, if, for loopJavaScript & TypeScript 2020. 11. 13. 17:40
1. String concatenation console.log('my' + ' cat'); // my cat console.log('1' + 2); // 12 console.log(`string literals: 1 + 2 = ${1 + 2}`); // 1 + 2 = 3 2. Numeric operators console.log(1 + 1); // add : 2 console.log(1 - 1); // substract : 0 console.log(1 / 1); // divide : 1 console.log(1 * 1); // multiply : 1 console.log(5 % 2); // remainder : 1 console.log(2 ** 3); // exponentiation : 8 3. Inc..