๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

200313

ยท ์•ฝ 9๋ถ„

React-Native ์‹คํ–‰ ํ™˜๊ฒฝ์„ค์ •#

๋งฅ(Mac)์— react native ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ตฌ์ถ•ํ•˜๊ธฐ

์ด ๋ธ”๋กœ๊ทธ์˜ ์„ค์ • ๋ฐฉ๋ฒ•์„ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ React-Native ์‹คํ–‰ํ™˜๊ฒฝ์„ ์„ค์ •ํ–ˆ๋‹ค.


  ![2020-03-13-200313-image-0](./images/2020-03-13-200313-image-0.png)
  <br />


  ![2020-03-13-200313-image-1](./images/2020-03-13-200313-image-1.png)
  <br />

์„ฑ๊ณต!

JavaScript ๋ฌธ๋ฒ•#

์‚ผํ•ญ์—ฐ์‚ฐ์ž#

condition ? true : false

  • ํ•œ ์ค„์ด ๋„ˆ๋ฌด ๊ธธ ๋•Œ
let text = array.length === 0 ? `๋ฐฐ์—ด์ด ๋น„์–ด์žˆ๋‹ค.` : `๋ฐฐ์—ด์ด ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค`;

Truthy and Falsy#

undefined ์™€ null ์€ ๋ชจ๋‘ false ๋กœ ๊ฐ„์ฃผ๋จ.

Falsy ํ•œ ๊ฐ’

  • undefined

  • null

  • 0

  • 
    
  • NaN

  • false

์ด ์™ธ์—๋Š” ๋ชจ๋‘ true ๋กœ ๊ฐ„์ฃผ๋จ

! , !! ๋ฅผ ํ™œ์šฉํ•ด Falsy ํ•œ ๊ฐ’๊ณผ Truthy ํ•œ ๊ฐ’์„ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ


๋‹จ์ถ• ํ‰๊ฐ€ ๋…ผ๋ฆฌ ๊ณ„์‚ฐ๋ฒ•#

A && B ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ A๊ฐ€ Truthy ํ•œ ๊ฐ’์ด๋ผ๋ฉด, B๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’์ด ๋˜๊ณ , Falsy ํ•œ ๊ฐ’์ด๋ผ๋ฉด, A๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

console.log(true && 'hello'); // helloconsole.log(false && 'hello'); // falseconsole.log('hello' && 'bye'); // byeconsole.log(null && 'hello'); // nullconsole.log(undefined && 'hello'); // undefinedconsole.log('' && 'hello'); // ''console.log(0 && 'hello'); // 0console.log(1 && 'hello'); // helloconsole.log(1 && 1); // 1

A || B ์—ฐ์‚ฐ์ž๋Š” ๋งŒ์•ฝ A๊ฐ€ Truthy ํ•œ ๊ฐ’์ด๋ผ๋ฉด, A๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’์ด ๋˜๊ณ , Falsy ํ•œ๊ฐ’์ด๋ผ๋ฉด B๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

console.log(true || 'hello'); // trueconsole.log(false || 'hello'); // helloconsole.log('hello' || 'bye'); // helloconsole.log(null || 'hello'); // helloconsole.log(undefined || 'hello'); // undefinedconsole.log('' || 'hello'); // helloconsole.log(0 || 'hello'); // helloconsole.log(1 || 'hello'); // 1console.log(1 || 1); // 1

ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ํŒŒ๋ผ๋ฏธํ„ฐ#

ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ = ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

function calculateCircleArea(r = 1) {    return Math.PI * r * r;}
const area = calculateCircleArea();console.log(area); // 3.141592653589793

์กฐ๊ฑด๋ฌธ ๋” ์Šค๋งˆํŠธํ•˜๊ฒŒ ์“ฐ๊ธฐ#

  • ํŠน์ • ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐ’ ์ค‘ ํ•˜๋‚˜์ธ์ง€ ํ™•์ธํ•ด์•ผ ํ•  ๋•Œ
function isAnimal(text) {    return (        text === '๊ณ ์–‘์ด' || text === '๊ฐœ' || text === '๊ฑฐ๋ถ์ด' || text === '๋„ˆ๊ตฌ๋ฆฌ'    );}
console.log(isAnimal('๊ฐœ')); // trueconsole.log(isAnimal('๋…ธํŠธ๋ถ')); // false

๋ฐฐ์—ด์˜ includes ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ

function isAnimal(name) {    const animals = ['๊ณ ์–‘์ด', '๊ฐœ', '๊ฑฐ๋ถ์ด', '๋„ˆ๊ตฌ๋ฆฌ'];    return animals.includes(name);}
console.log(isAnimal('๊ฐœ')); // trueconsole.log(isAnimal('๋…ธํŠธ๋ถ')); // false

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ

const isAnimal = (name) => ['๊ณ ์–‘์ด', '๊ฐœ', '๊ฑฐ๋ถ์ด', '๋„ˆ๊ตฌ๋ฆฌ'].includes(name);
console.log(isAnimal('๊ฐœ')); // trueconsole.log(isAnimal('๋…ธํŠธ๋ถ')); // false

  • ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•  ๋•Œ
function getSound(animal) {    if (animal === '๊ฐœ') return '๋ฉ๋ฉ!';    if (animal === '๊ณ ์–‘์ด') return '์•ผ์˜น~';    if (animal === '์ฐธ์ƒˆ') return '์งน์งน';    if (animal === '๋น„๋‘˜๊ธฐ') return '๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ';    return '...?';}

switch ์‚ฌ์šฉ

function getSound(animal) {    switch (animal) {        case '๊ฐœ':            return '๋ฉ๋ฉ!';        case '๊ณ ์–‘์ด':            return '์•ผ์˜น~';        case '์ฐธ์ƒˆ':            return '์งน์งน';        case '๋น„๋‘˜๊ธฐ':            return '๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ';        default:            return '...?';    }}
console.log(getSound('๊ฐœ')); // ๋ฉ๋ฉ!console.log(getSound('๋น„๋‘˜๊ธฐ')); // ๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ

๊ฐ์ฒด ํ™œ์šฉ

function getSound(animal) {    const sounds = {        ๊ฐœ: '๋ฉ๋ฉ!',        ๊ณ ์–‘์ด: '์•ผ์˜น~',        ์ฐธ์ƒˆ: '์งน์งน',        ๋น„๋‘˜๊ธฐ: '๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ',    };    return sounds[animal] || '...?';}
console.log(getSound('๊ฐœ')); // ๋ฉ๋ฉ!console.log(getSound('๋น„๋‘˜๊ธฐ')); // ๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ

  • ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ฝ”๋“œ ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•  ๋•Œ
function makeSound(animal) {    const tasks = {        ๊ฐœ() {            console.log('๋ฉ๋ฉ');        },        ๊ณ ์–‘์ด() {            console.log('๊ณ ์–‘์ด');        },        ๋น„๋‘˜๊ธฐ() {            console.log('๊ตฌ๊ตฌ ๊ตฌ ๊ตฌ');        },    };    if (!tasks[animal]) {        console.log('...?');        return;    }    tasks[animal]();}
makeSound('๊ฐœ');makeSound('๋น„๋‘˜๊ธฐ');

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น (๊ตฌ์กฐ๋ถ„ํ•ด) ๋ฌธ๋ฒ•#

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น#

const object = {a: 1, b: 2};
const {a, b} = object;
console.log(a); // 1console.log(b); // 2
  • ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ
const object = {a: 1, b: 2};
function print({a, b}) {    console.log(a);    console.log(b);}
print(object);

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์‹œ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •#

const object = {a: 1};
const {a, b = 2} = object;
console.log(a); // 1console.log(b); // 2
  • ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ
const object = {a: 1};
function print({a, b = 2}) {    console.log(a);    console.log(b);}
print(object);// 1// 2

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์‹œ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ#

const animal = {    name: '๋ฉ๋ฉ์ด',    type: '๊ฐœ',};
const nickname = animal.name;
console.log(nickname); // ๋ฉ๋ฉ์ด

animal.name ๊ฐ’์„ nickname ๊ฐ’์— ๋‹ด๊ณ  ์žˆ๋‹ค. ์ด๋ฅผ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด,

const animal = {    name: '๋ฉ๋ฉ์ด',    type: '๊ฐœ',};
const {name: nickname} = animal;console.log(nickname);

: ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์ค„ ์ˆ˜ ์žˆ๋‹ค.


๋ฐฐ์—ด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น#

const array = [1, 2];const [one, two] = array;
console.log(one);console.log(two);

๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ์›์†Œ๋ฅผ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์ƒˆ๋กœ ์„ ์–ธํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉ. ๊ฐ์ฒด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •์ด ๊ฐ€๋Šฅ.

const array = [1];const [one, two = 2] = array;
console.log(one);console.log(two);

๊นŠ์€ ๊ฐ’ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น#

const deepObject = {    state: {        information: {            name: 'younho9',            languages: ['korean', 'english', 'chinese'],        },    },    value: 5,};

์—ฌ๊ธฐ์„œ name , languages , value ๊ฐ’์„ ๋ฐ–์œผ๋กœ ๊บผ๋‚ด๊ณ  ์‹ถ์„ ๋•Œ

const deepObject = {    state: {        information: {            name: 'younho9',            languages: ['korean', 'english', 'chinese'],        },    },    value: 5,};
const {name, languages} = deepObject.state.information;const {value} = deepObject;
const extracted = {    name,    languages,    value,};
console.log(extracted); // {name: "younho9", languages: Array[3], value: 5}

์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

const extracted = {    name: name,    languages: languages,    value: value,};

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€

const deepObject = {    state: {        information: {            name: 'younho9',            languages: ['korean', 'english', 'chinese'],        },    },    value: 5,};
const {    state: {        information: {name, languages},    },    value,} = deepObject;
const extracted = {    name,    languages,    value,};
console.log(extracted);

์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋‹ค๋ฃจ๊ธฐ#

2020-03-13-200313-image-2

function work() {    const start = Date.now();    for (let i = 0; i < 10000000; i++) {}    const end = Date.now();    console.log(end - start + 'ms');}
work();console.log('๋‹ค์Œ ์ž‘์—…');

work() ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์•ˆ ๋‹ค์Œ ์ž‘์—…์ด ์ง„ํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

์ด๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค. โ†’ setTImeout() ํ•จ์ˆ˜ ์‚ฌ์šฉ

function work() {    setTimeout(() => {        const start = Date.now();        for (let i = 0; i < 1000000000; i++) {}        const end = Date.now();        console.log(end - start + 'ms');    }, 0);}
console.log('์ž‘์—… ์‹œ์ž‘!');work();console.log('๋‹ค์Œ ์ž‘์—…');

๋จผ์ € work() ์ดํ›„์˜ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๊ณ  work() ๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•œ๋‹ค.

๋งŒ์•ฝ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ work() ํ•จ์ˆ˜๊ฐ€ ๋๋‚œ ์ดํ›„์— ์–ด๋–ค ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

function work(callback) {    setTimeout(() => {        const start = Date.now();        for (let i = 0; i < 1000000000; i++) {}        const end = Date.now();        console.log(end - start + 'ms');        callback();    }, 0);}
console.log('์ž‘์—… ์‹œ์ž‘!');work(() => {    console.log('์ž‘์—…์ด ๋๋‚ฌ์–ด์š”!');});console.log('๋‹ค์Œ ์ž‘์—…');

work() ๊ฐ€ ๋๋‚œ ๋’ค์— ์ˆ˜ํ–‰ํ•  ํ•จ์ˆ˜(์ž‘์—…)๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ค€๋‹ค.


Promise#

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ๋น„๋™๊ธฐ ์ž‘์—…์ด ๋งŽ์•„์งˆ ๋•Œ, ๋ชจ๋‘ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋‚œ์žกํ•ด์ง€๊ฒŒ๋œ๋‹ค.

function increaseAndPrint(n, callback) {    setTimeout(() => {        const increased = n + 1;        console.log(increased);        if (callback) {            callback(increased);        }    }, 1000);}
increaseAndPrint(0, (n) => {    increaseAndPrint(n, (n) => {        increaseAndPrint(n, (n) => {            increaseAndPrint(n, (n) => {                increaseAndPrint(n, (n) => {                    console.log('๋!');                });            });        });    });});

์ด๋ฅผ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ES6์— ๋„์ž…๋œ ๊ธฐ๋Šฅ์ด Promise ์ด๋‹ค.

Promise ๋Š” ์„ฑ๊ณตํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์‹คํŒจํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์„ฑ๊ณตํ•  ๋•Œ๋Š” resolve ๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๊ณ , ์‹คํŒจํ•  ๋•Œ๋Š” reject ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

const myPromise = new Promise((resolve, reject) => {    setTimeout(() => {        resolve(1);    }, 1000);});
myPromise.then((n) => {    console.log(n);});