#
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 Falsyundefined
์ 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);
์ด๋ ๊ฒ ํ๋ ๋ฐฉ๋ฒ๋ ์๋ค.
#
์๋ฐ์คํฌ๋ฆฝํธ์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ ๋ค๋ฃจ๊ธฐ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);});