ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

** โš  priv | ๊ฐ•์˜๋…ธํŠธ ์นดํ…Œ๊ณ ๋ฆฌ์— ์žˆ๋Š” ๋‚ด์šฉ์€ ๋ฐœํ–‰์ด ๋ชฉ์ ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ „ํ˜€ ์ •์ œ๋˜์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹น.**

 

์„น์…˜ 15:Appendix II: Intermediate Javascript

207. Advanced Functions

Closures

a function ran. the function executed. It’s never going to execute again.
BUT it’s going remember that there reference to those variables.
so the child scope always access to the parent scope.

Currying

const multiply  = (a,b) => a*b;
const curriedMultiply = (a) => (b) => a*b;
curriedMultiply(3);    // this part returns a function "(b) => 3*b"

Compose

const compose = (f,g) => (a) => f(g(a));

const sum = (num) => num + 1;

compose(sum, sum)(5);    // result : 7

+

Avoiding Side Effects, functional purity!

avoiding side effects and always returning we create something that we call deterministic .

deterministic - ๊ฒฐ์ •๋ก ์ 
๊ฐ™์€ input์ด๋ฉด ํ•ญ์ƒ same value ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

It's a very important concept and that's a key principle in avoiding bugs.

 

208. Advanced Arrays

square brackets []

map, filter, reduce

pure simple functions

It always returns of value.
And there are no side effects.

we’re not changing the array.
just making a new copy of the array.
never mutating the data

 

209. Advanced Objects

  1. reference type

  2. context vs scope

  3. instantiation

class Player {
  constructor(name, type) {
    console.log("Player ", this);
    this.name = name;
    this.type = type;
  }
  introduce() {
    console.log(`Hi I am ${this.name}, I am a ${this.type}`);
  }
}

class Wizard extends Player {
  constructor(name, type) {
    //console.log("Wizard ", this); // ์ด๋ ‡๊ฒŒ์“ฐ๋ฉด ์—๋Ÿฌ๋‚จ
    // Uncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
    super(name, type);
    console.log("Wizard ", this);
  }
  play() {
    console.log(`WEEEEEE I'm a ${this.type}`);
  }
}

const wizard1 = new Wizard("Judy", "Dark Wizard");
const wizard2 = new Wizard("Nick", "Healer");
wizard1.play(); // WEEEEEE I'm a Dark Wizard
wizard2.introduce(); // Hi I am Nick, I am a Healer

Uncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor

ํŒŒ์ƒ๋œ ํด๋ž˜์Šค์—์„œ this ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด
super ์ƒ์„ฑ์ž๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ•ด์•ผ์ง€๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

// classical inheritance

var Player = function(name, type) {
    this.name = name;
    this.type = type;
}

Player.prototype.introduce = function {
    console.log(`Hi I am ${this.name}, I am a ${this.type}`);
}

var wizard1 = new Player("Judy", "Dark Wizard");
var wizard2 = new Player("Nick", "Healer");

wizard1.play = function(){
    console.log(`WEEEEEE I'm a ${this.type}`);
};

wizard2.play = function(){
    console.log(`WEEEEEE I'm a ${this.type}`);
};

 

210. ES7

ES7์€ ๋ณ„๊ฑฐ ์—†์Œ

ES6๋Š” ๋ญ๊ฐ€ ๋งŽ์ด ๋ฐ”๋€Œ์—ˆ์—ˆ๋Š”๋ฐ ES7์€ ๋‘๊ฐœ ์ถ”๊ฐ€๋Œ

  1. .includes() method

added on string and array

const pets = ["cat", "dog", "bat"];
pets.includes("dog");    // true
pets.includes("bird");    // false

"Hello".includes("o");    // true
"Hello".includes("a");    // false
  1. exponential operator

const square = (x) => x**2;
square(2);    // 4
square(5);    // 25

const cube = (y) => y**3;
cube(3);    // 27
cube(4);    // 64

console.log(10 ** -2);    // 0.01

The exponentiation operator (**) returns the result of raising the first operand to the power of the second operand.

 

211. ES8

ES7๋ณด๋‹ค๋Š” ์ข€๋”๋ฐ”๋€Œ๊ธดํ•จ
์ค‘์š”ํ•œ ๋ช‡๊ฐœ๋งŒ ์‚ดํŽด๋ด„

  1. padStart

str.padStart(targetLength [, padString])

.padEnd()

'abc'.padStart(10);         // "       abc"
'abc'.padStart(10, "foo");  // "foofoofabc"
'abc'.padStart(6,"123465"); // "123abc"
'abc'.padStart(8, "0");     // "00000abc"
'abc'.padStart(1);          // "abc"
  1. Trailing commas in functions

ECMAScript 2017 allows trailing commas in function parameter lists.

the ending comma is now valid

function f(p) {}
function f(p,) {} 

(p) => {};
(p,) => {};

์›๋ž˜๋Š” ๋ฐฐ์—ด๋ฆฌํ„ฐ๋Ÿด์—์„œ๋งŒ ํ—ˆ์šฉ๋์—ˆ๊ณ ,
ES5์—์„œ ๊ฐ์ฒด๋ฆฌํ„ฐ๋Ÿด์—์„œ๋„ ํ—ˆ์šฉ๋จ,
ES7๋ถ€ํ„ฐ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—๋„ ํ—ˆ์šฉ๋จ

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Trailing_commas

-> ๋ฒ„์ „ ๊ด€๋ฆฌ ์ด๋ ฅ์ด ๊ฐ„๋‹จํ•ด์ง€๊ณ  ์ฝ”๋“œ ํŽธ์ง‘์ด ๋” ํŽธํ•ด์ง„๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  1. Object.values, Object.entries

Object.values : ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๋Š” (์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ) ์†์„ฑ์˜ ๊ฐ’๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Object.entries : ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๋Š” (์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ) ์†์„ฑ์˜ [key, value] ์Œ์˜ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Obj ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์‚ฌ์šฉ

let obj = {
  username0 : "Santa",
  username1 : "Rudolf",
  username2 : "Mr.Grinch",
}

Object.keys(obj).forEach((key, index) => {
  console.log(key, obj[key], index);
});
// username0 Santa 0
// username1 Rudolf 1
// username2 Mr.Grinch 2

Object.values(obj).forEach((value) => {
  console.log(value);
});
// Santa
// Rudolf
// Mr.Grinch

Object.entries(obj);
// [ 
//   ["username0", "Santa"], 
//   ["username1", "Rudolf"], 
//   ["username2", "Mr.Grinch"]
// ]
Object.entries(obj).map(val => val[1]+val[0].replace("username", ""));

//["Santa0", "Rudolf1", "Mr.Grinch2"]

 

213. ES10 (ES2019)

arr.flat()

nested Array -> flat Array (์ค‘์ฒฉ๋ฐฐ์—ด ํ‰ํƒ„ํ™”)

[1,[2,3],[4,5]].flat()

// [ 1,2,3,4,5 ]
[1, 2,[3,4,[5]]].flat()

// [ 1, 2, 3, 4, [5] ]

[1, 2,[3,4,[5]]].flat(2)
// [1, 2, 3, 4, 5]

arr.flat(depth)
depth๊ธฐ๋ณธ๊ฐ’์€ 1

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

const arr5 = [1, 2, , , 5, 6];
arr5.flat();
// [1, 2, 5, 6] ๋ฐฐ์—ด์˜ ๊ตฌ๋ฉ๋„ ์ œ๊ฑฐ
// Turn the trapped 3 number into: [3]
const trapped = [[[[[[[[[[[[[[[[[[[[[[[[[[3]]]]]]]]]]]]]]]]]]]]]]]]]];

//Solution
console.log(trapped.flat(Infinity))
// Infintiy is actually a LARGE number in JavaScipt. 
// It represents the maximum amount of memory that we can hold for a number! 
// Learn more here: 
// https://riptutorial.com/javascript/example/2337/infinity-and--infinity
const jurassicPark = [
  ["๐ŸฆŽ", "๐Ÿฆ‡"],
  "๐Ÿ",
  "๐Ÿข",
  [["๐Ÿ", "๐Ÿฆ‚"]],
  [[[["๐Ÿฆ–"]]]],
  ["๐Ÿ™", "๐Ÿฆ"]
];

jurassicPark.flat(50);
// (9) ["๐ŸฆŽ", "๐Ÿฆ‡", "๐Ÿ", "๐Ÿข", "๐Ÿ", "๐Ÿฆ‚", "๐Ÿฆ–", "๐Ÿ™", "๐Ÿฆ"]

arr.flatMap()

์ด๋Š” ๊นŠ์ด 1์˜ flat ์ด ๋’ค๋”ฐ๋ฅด๋Š” map ๊ณผ ๋™์ผํ•˜์ง€๋งŒ, flatMap ์€ ์•„์ฃผ ์œ ์šฉํ•˜๋ฉฐ ๋‘˜์„ ํ•˜๋‚˜์˜ ๋ฉ”์†Œ๋“œ๋กœ ๋ณ‘ํ•ฉํ•  ๋•Œ ์กฐ๊ธˆ ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

const jurassicParkChaos = jurassicPark.flatMap(
  creature => creature + "๐Ÿฆ–"
);

// jurassicParkChaos
//(6) ["๐ŸฆŽ,๐Ÿฆ‡๐Ÿฆ–", "๐Ÿ๐Ÿฆ–", "๐Ÿข๐Ÿฆ–", "๐Ÿ,๐Ÿฆ‚๐Ÿฆ–", "๐Ÿฆ–๐Ÿฆ–", "๐Ÿ™,๐Ÿฆ๐Ÿฆ–"]

str.trimStart()

const userEmail1 = "        judy@email.com";
const userEmail2 = "nick@email.com        ";

userEmail1.trimStart();
// "judy@email.com"

userEmail2.trimEnd();
// "nick@email.com"

Object.fromEntries

const inventory = [
  ["potato", 23],
  ["apple", 40],
  ["broccoli", 15]
];

const obj = Object.fromEntries(inventory);
// {potato: 23, apple: 40, broccoli: 15}

Object.entries(obj);
// ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด inventory๋ž‘ ๊ฐ™์€ ๋ชจ์–‘์œผ๋กœ ๋‹ค์‹œ ๋ฐ”๋€œ
// ๊ธ๊ป˜ fromEntries <-> entries ์ด๋Ÿฐ ๋Šฌ๋‚Œ

try & catch

catch(error) ์—์„œ error ํŒŒ๋ผ๋ฏธํ„ฐ ์ƒ๋žต๊ฐ€๋Šฅ

214. Advanced Loops

for of

iterating - array, strings
we are able to iterate over individual items

for (item of obj){ console.log(item);} 
// Uncaught TypeError: obj is not iterable

for in - properties

enumerating - object

enumerable
( enumerate : ์—ด๊ฑฐํ•˜๋‹ค)

it allows us to see the property

216. Modules

code reusablility

global namespace

dependency resolution

Inline Script -> Script Tags -> IIFE ->

Common JS + browserify

// js1
module.exports = function add (a,b){
    return a + b;
};

// js2
var add = require("./add");

module bundler

ES6 + Webpack2

// js1
export const add = (a,b) => A + b;
// or
export default function add() {
    return a + b;
}

// js2
import { add } from "./add";
// or
import add from "./add";

 

์ถœ์ฒ˜ : Udemy - JavaScript: The Advanced Concept
https://www.udemy.com/course/advanced-javascript-concepts

๋Œ“๊ธ€