์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- SnapKit
- ํ๋ก๊ทธ๋๋จธ์ค
- ios
- realm
- reactorkit
- Kuring
- UIKit
- TCA
- MVVM
- arkit
- SwiftUI
- RxSwift
- CollectionView
- tableView
- BFS
- raywenderlich
- visionOS
- Lv2
- Xcode
- rxcocoa
- designpattern
- node.js
- Flutter
- ํจ์คํธ์บ ํผ์ค
- Swfit
- swift
- ๋ฐฑ์ค
- combine
- BOJ
- XCTest
- Today
- Total
lgvv98
[Node.js] #2 ์์๋ฌ์ผ ํ ์๋ฐ์คํฌ๋ฆฝํธ ๋ณธ๋ฌธ
#2 ์์๋ฌ์ผ ํ ์๋ฐ์คํฌ๋ฆฝํธ
ES2015(ES6) ์ดํ๋ฅผ ๊ธฐ์ค์ผ๋ก ํฉ๋๋ค.
๋ณ์๋ฅผ ์ ์ธํ๋ ๋ฐฉ๋ฒ
if (true) {
var x = 3;
}
console.log(x); // 3
if (true) {
const y = 3;
}
console.log(y); // Uncaught ReferenceError: y is not defined
var๋ ํจ์ ์ค์ฝํ๋ฅผ ๊ฐ์ ธ์ if๋ฌธ์ ๋ธ๋ก๊ณผ ๊ด๊ณ์์ด ์ ๊ทผ ๊ฐ๋ฅ.
const์ let์ ๋ธ๋ก ์ค์ฝํ๋ฅผ ๊ฐ์ ธ์ ๋ธ๋ก ๋ฐ์์๋ ๋ณ์์ ์ ๊ทผ ๋ถ๊ฐ.
const a = 0;
a = 1; // Uncaught TypeError: Assignment to constant variable.
let b = 0;
b = 1; // 1
const c; // Uncaught SyntaxError: Missing initializer in const declaration
const๋ ์ด๊ธฐ๊ฐ ์ฃผ์ด์ผํ๋ฉฐ ๊ฐ ๋ณ๊ฒฝ ๋ถ๊ฐ. (swift๋ก ์น๋ฉด static let)
let์ ๊ฐ ๋ณ๊ฒฝ ๊ฐ๋ฅ
# ํ ํ๋ฆฟ ๋ฌธ์์ด
์๋ ์์๊ฐ ์๋๋ฐ ๋ฐฑํฑ์ผ๋ก ์คํธ๋ง์ ๋ฌถ๋๊ฑฐ ์ธ์๋ ํฌ๊ฒ swift๋ ๋ค๋ฅธ๊ฑด ์๋ณด์
// ๊ธฐ์กด ES5 ๋ฌธ๋ฒ
var num1 = 1;
var num2 = 2;
var result = 3;
var string1 = num1 + ' ๋ํ๊ธฐ ' + num2 + '๋ \'' + result + '\'';
console.log(string1); // 1 ๋ํ๊ธฐ 2๋ '3'
// ES2015(ES6) ๋ฌธ๋ฒ
const num3 = 1;
const num4 = 2;
const result2 = 3;
const string2 = `${num3} ๋ํ๊ธฐ ${num4}๋ '${result2}'`;
console.log(string2); // 1 ๋ํ๊ธฐ 2๋ '3'
# ๊ฐ์ฒด ๋ฆฌํฐ๋ด
sayJs ๊ฐ์ ๊ฐ์ฒด์ ๋ฉ์๋์ ํจ์๋ฅผ ์ฐ๊ฒฐํ ๋ ๋๋ ์ฝ๋ก ๊ณผ function์ ๋ถ์ด์ง ์์๋ ๋๋ค.
// ๊ธฐ์กด
var sayNode = function() {
console.log('Node');
};
var es = 'ES';
var oldObject = {
sayJS: function() {
console.log('JS');
},
sayNode: sayNode,
};
oldObject[es + 6] = 'Fantastic';
oldObject.sayNode(); // Node
oldObject.sayJS(); // JS
console.log(oldObject.ES6); // Fantastic
// ์๋กญ๊ฒ ๋ฑ์ฅ
const newObject = {
sayJS() {
console.log('JS');
},
sayNode,
[es + 6]: 'Fantastic',
};
newObject.sayNode(); // Node
newObject.sayJS(); // JS
console.log(newObject.ES6); // Fantastic
# ํ์ดํ ํจ์
ํ์ดํ ํจ์์์๋ function๋์ => ๊ธฐํธ๋ก ํจ์๋ฅผ ์ ์ธ.
ํ์ดํํจ์์์๋ ๋ด๋ถ์ return๋ฐ์ ์๋ ๊ฒฝ์ฐ ์ฝ๋๋ฅผ ์ค์ผ ์ ์์.
// add1, 2, 3, 4๋ ๋ชจ๋ ๊ฐ์ ๊ธฐ๋ฅ
function add1(x, y) {
return x + y;
}
const add2 = (x, y) => {
return x + y;
};
const add3 = (x, y) => x + y;
const add4 = (x, y) => (x + y);
// not๋ ๊ฐ์ ๊ธฐ๋ฅ
function not1(x) {
return !x;
}
const not2 = x => !x;
๊ธฐ์กด functionrhk ๋ค๋ฅธ this์ ๋ฐ์ธ๋ฉ ๋ฐ์์ ๋ณด์.
- this๋ ์๋ฐ์์ ๋ง์ด ๋ณด์๊ณ , ์ค์ํํธ์ self๋ ๋ฎ์์ผ๋ฉฐ forEach์ => ๋ in์ผ๋ก ๋๊ปด์ง๊ณ { } ๋ ์์ด์ผ ํ๋ ์ค์ํํธ๋ ๋ฌ๋ฆฌ ๊ฐ์ ๋ฐ์์ ๊บผ๋ด์ ์ ์ํ๋๋ฏ.
var relationship1 = {
name: 'zero',
friends: ['nero', 'hero', 'xero'],
logFriends: function () {
var that = this; // relationship1์ ๊ฐ๋ฆฌํค๋ this๋ฅผ that์ ์ ์ฅ
this.friends.forEach(function (friend) {
console.log(that.name, friend);
});
},
};
relationship1.logFriends();
const relationship2 = {
name: 'zero',
friends: ['nero', 'hero', 'xero'],
logFriends() {
this.friends.forEach(friend => {
console.log(this.name, friend);
});
},
};
relationship2.logFriends();
# ๊ตฌ์กฐ ๋ถํด ํ ๋น
๊ฐ์ฒด์ ๋ฐฐ์ด๋ก๋ถํฐ ์์ฑ์ด๋ ์์๋ฅผ ์ฝ๊ฒ ๊บผ๋ผ ์ ์์.
๊ตฌ์กฐ ๋ถํด ํ ๋น์ ์ฌ์ฉํ๋ฉด ํจ์์ this๊ฐ ๋ฌ๋ผ์ง ์ ์์. ๋ฌ๋ผ์ง this๋ฅผ ์๋๋๋ก ๋๋๋ ค์ฃผ๋ ค๋ฉด getCandyํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
var candyMachine = {
status: {
name: 'node',
count: 5,
},
getCandy: function () {
this.status.count--;
return this.status.count;
},
};
var getCandy = candyMachine.getCandy;
var count = candyMachine.status.count;
// ์์ ์ฝ๋๋ฅผ ์๋์ฒ๋ผ๋ ์์ฑ ๊ฐ๋ฅ.
const candyMachine = {
status: {
name: 'node',
count: 5,
},
getCandy() {
this.status.count--;
return this.status.count;
},
};
const { getCandy, status: { count } } = candyMachine;
๋ฐฐ์ด์ ๋ํ ๊ตฌ์กฐ ๋ถํด ํ ๋น ๋ฐฉ๋ฒ๋ ์กด์ฌํ๋๋ฐ,
var array = ['nodejs', {}, 10, true];
var node = array[0];
var obj = array[1];
var bool = array[3];
const array = ['nodejs', {}, 10, true];
const [node, obj, , bool] = array;
# ํด๋์ค
๋ค๋ฅธ ์ธ์ด์ฒ๋ผ ํด๋์ค ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ ๊ฒ์ด ์๋๋ผ ์ฌ์ ํ ํ๋กํ ํ์ ๊ธฐ๋ฐ์ผ๋ก ๋์.
// ๊ธฐ์กด์ ํ๋กํ ํ์
์์ ์์ ์ฝ๋
var Human = function(type) {
this.type = type || 'human';
};
Human.isHuman = function(human) {
return human instanceof Human;
}
Human.prototype.breathe = function() {
alert('h-a-a-a-m');
};
var Zero = function(type, firstName, lastName) {
Human.apply(this, arguments);
this.firstName = firstName;
this.lastName = lastName;
};
Zero.prototype = Object.create(Human.prototype);
Zero.prototype.constructor = Zero; // ์์ํ๋ ๋ถ๋ถ
Zero.prototype.sayName = function() {
alert(this.firstName + ' ' + this.lastName);
};
var oldZero = new Zero('human', 'Zero', 'Cho');
Human.isHuman(oldZero); // true
// ์๋๋ ํด๋์ค ๊ธฐ๋ฐ
class Human {
constructor(type = 'human') {
this.type = type;
}
static isHuman(human) {
return human instanceof Human;
}
breathe() {
alert('h-a-a-a-m');
}
}
class Zero extends Human {
constructor(type, firstName, lastName) {
super(type);
this.firstName = firstName;
this.lastName = lastName;
}
sayName() {
super.breathe();
alert(`${this.firstName} ${this.lastName}`);
}
}
const newZero = new Zero('human', 'Zero', 'Cho');
Human.isHuman(newZero); // true
ํ๋กํ ํ์ ๊ธฐ๋ฐ ์์ ๋ ์ ์๋ฟ์ง ์์. ํ์ง๋ง ํด๋์ค ๊ธฐ๋ฐ ์ฝ๋๋ constructor๊ฐ ์ค์ํํธ์ init + ๋ณ์์ ์ธ์ ๋ด๋นํ๊ณ sayName์ func ๊ทธ๋ฆฌ๊ณ super๋ฅผ ํตํด ์์๋ฐ์ ๊ฒ์ ์ฌ์ฉํ๋ฉฐ $ { } ์์๋ constructor์์ ์ ์ํ ๊ฑธ ์ฌ์ฉํ๋ ๊ฒ ๊ฐ์.
ํ์ ์ string์ผ๋ก ์ ์ํ๋๊ฒ ์ข ํน์ดํด๋ณด์.
# ํ๋ก๋ฏธ์ค
์๋ฐ์คํฌ๋ฆฝํธ์ ๋ ธ๋์ API๋ค์ด ์ฝ๋ฐฑ ๋์ ํ๋ก๋ฏธ์ค ๊ธฐ๋ฐ์ผ๋ก ์ฌ๊ตฌ์ฑ.
์ ๋ช ๋์ ์ฝ๋ฐฑ์ง์ฅ ํ์ ๊ทน๋ณตํ๋ค๋ ํ๊ฐ.
๋ค๋ง ํ๋ก๋ฏธ์ค๋ ๊ท์น์ด ์์. ๋จผ์ ํ๋ก๋ฏธ์ค ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ ํจ.
const condition = true; // true๋ฉด resolve, false๋ฉด reject
const promise = new Promise((resolve, reject) => {
if (condition) {
resolve('์ฑ๊ณต');
} else {
reject('์คํจ');
}
});
// ๋ค๋ฅธ ์ฝ๋๊ฐ ๋ค์ด๊ฐ ์ ์์
promise
.then((message) => {
console.log(message); // ์ฑ๊ณต(resolve)ํ ๊ฒฝ์ฐ ์คํ
})
.catch((error) => {
console.error(error); // ์คํจ(reject)ํ ๊ฒฝ์ฐ ์คํ
})
.finally(() => { // ๋๋๊ณ ๋ฌด์กฐ๊ฑด ์คํ
console.log('๋ฌด์กฐ๊ฑด');
});
ํ๋ก๋ฏธ์ค์ ๋ด๋ถ์์ resolve๊ฐ ํธ์ถ๋๋ฉด then์ผ๋ก ๊ทธ๋ ์ง ์์ผ๋ฉด catch๋ก
finally๋ ๋ฌด์กฐ๊ฑด ์คํ.
new Promise๋ ๋ฐ๋ก ์คํ๋๋ ๊ฒฐ๊ด๊ฐ์ then์ ๋ถ์์ ๊ฒฝ์ฐ์๋ง ๋ฐ์ ์ ์์.
์๋ ์ฝ๋๋ ์ฌ๋ฌ๋ฒ ํธ์ถํ๋ ์์
promise
.then((message) => {
return new Promise((resolve, reject) => {
resolve(message);
});
})
.then((message2) => {
console.log(message2);
return new Promise((resolve, reject) => {
resolve(message2);
});
})
.then((message3) => {
console.log(message3);
})
.catch((error) => {
console.error(error);
});
// ์คํ ์ ๋ฆฌ
์ฒ์ then์์ resolve๋ฅผ ํ๋ฉด ๋ค์ then์์ message2๋ก ๋ฐ์ ์ ์์.
๋ค์ message2๊ฐ resolveํ ๊ฒ์ message3๋ก ๋ฐ์.
๋จ, then์์ new Promise๋ฅผ returnํด์ผ๋ง ๋ค์ then์์ ๋ฐ์ ์ ์์.
์๋ ์ฝ๋๋ ์ฝ๋ฐฑ์ ์ฐ๋ ํจํด์ ๋ํ ์ ๋ฆฌ
๊ฐ์ฅ ๋์ ๋๋ ์ฐจ์ด๋ ์๋ฌ๋ฅผ ํ๋ฒ์ ์ฒ๋ฆฌํ๋์ง ์๋๋ฉด ๊ฐ๊ฐ ๋์ํด์ฃผ๋์ง ์ฌ๋ถ.
// ์ฝ๋ฐฑํจ์ 3์ค์ฒฉ. -> ๊ฐ ์ฝ๋ฐฑ๋ง๋ค ์๋ฌ๋ ๋ฐ๋ก ์ฒ๋ฆฌํด์ผ ํ๋ค.
function findAndSaveUser(Users) {
Users.findOne({}, (err, user) => { // ์ฒซ ๋ฒ์งธ ์ฝ๋ฐฑ
if (err) {
return console.error(err);
}
user.name = 'zero';
user.save((err) => { // ๋ ๋ฒ์งธ ์ฝ๋ฐฑ
if (err) {
return console.error(err);
}
Users.findOne({ gender: 'm' }, (err, user) => { // ์ธ ๋ฒ์งธ ์ฝ๋ฐฑ
// ์๋ต
});
});
});
}
// ์์ ์ฝ๋๋ฅผ ๋ณํ
function findAndSaveUser(Users) {
Users.findOne({})
.then((user) => {
user.name = 'zero';
return user.save();
})
.then((user) => {
return Users.findOne({ gender: 'm' });
})
.then((user) => {
// ์๋ต
})
.catch(err => {
console.error(err);
});
}
์์ ์์ ์์๋ ์ฝ๋์ ๊น์ด๊ฐ ๋์ด์ ๊น์ด์ง์ง ์์. ๊ฐ๊ฐ error๋ฅผ ์ฒ๋ฆฌํ๋ ๊ฒ๋ catch ํ๋ฒ์ผ๋ก ์ฒ๋ฆฌ ๊ฐ๋ฅ.
ํ๋ก๋ฏธ์ค ์ฌ๋ฌ๊ฐ๋ฅผ ํ๋ฒ์ ์คํํ ์ ์๋ ๋ฐฉ๋ฒ๋ ์กด์ฌ. (Pomise.all)
const promise1 = Promise.resolve('์ฑ๊ณต1');
const promise2 = Promise.resolve('์ฑ๊ณต2');
Promise.all([promise1, promise2])
.then((result) => {
console.log(result); // ['์ฑ๊ณต1', '์ฑ๊ณต2'];
})
.catch((error) => {
console.error(error);
});
๊ทผ๋ฐ all์ ๊ฒฝ์ฐ์๋ ๋ฐฐ์ด์ ๋ค์ด๋ ๊ฒ์ค ํ๋๋ง reject์ฌ๋ catch๋ก ๋์ด๊ฐ์ ์ด๋ค ์น๊ตฌ๊ฐ reject๋์๋์ง ๋ชจ๋ฆ.
allSettled๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ฒ๋ผ ์ฌ์ฉ ๊ฐ๋ฅ.
const promise1 = Promise.resolve('์ฑ๊ณต1');
const promise2 = Promise.resolve('์คํจ2');
const promise3 = Promise.resolve('์ฑ๊ณต3');
Promise.allSettled([promise1, promise2, promise3])
.then((result) => {
console.log(result);
/*
[
{ status: 'fulfilled', value: '์ฑ๊ณต1' },
{ status: 'rejected', reason: '์คํจ2' },
{ status: 'fulfilled', value: '์ฑ๊ณต3' }
]
*/
})
.catch((error) => {
console.error(error);
});
์ด๋ค ํ๋ก๋ฏธ์ค๊ฐ reject๊ฐ ๋์๋์ง ํ์ธํ ์ ์์.
์ฐธ๊ณ ๋ก Node 16๋ถํฐ๋ reject๋ promise์ catch๋ฅผ ๋ฌ์ง ์์ผ๋ฉด UnhandledPromiseRejection ์๋ฌ ๋ฐ์.
์๋ฌ ๋ฐ์ ์, ๋ค์ ์ฝ๋๊ฐ ์คํ๋์ง ์์.
# async/await
Promise๊ฐ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋ฐฑ์ง์ฅ ํด๊ฒฐํ์ด๋ ์ฌ์ ํ ์ฝ๋๊ฐ ์ฅํฉํจ.
ES2017๋ถํฐ ์ฌ์ฉํ ์ ์์.
// ์ด ์ฝ๋๋ฅผ async/awiat๋ฅผ ํตํด ๊ต์ฒดํด๋ณผ ์์
function findAndSaveUser(Users) {
Users.findOne({})
.then((user) => {
user.name = 'zero';
return user.save();
})
.then((user) => {
return Users.findOne({ gender: 'm' });
})
.then((user) => {
// ์๋ต
})
.catch(err => {
console.error(err);
});
}
// async/await ์์
async function findAndSvaeUser(Users) {
let user = await Users.findOne({});
user.name = 'zero';
user = await Users.save();
user = await Users.findOne({ gender: 'm' });
// ์๋ต
}
์ ์ด๊ฑธ ๋ณด๋๊น Swift์์ async/await ์ฌ์ฉํ๋๋ฐ ์ดํด๊ฐ ๋ ๊น์ด์ง๋ ๊ธฐ๋ถ์ด๋ค.
๋ค๋ง ์ ์ฝ๋๋ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ถ๋ถ(reject ๊ฒฝ์ฐ)์ด ์์ผ๋ฏ๋ก ์ถ๊ฐ์ ์ธ ์์ ์ด ํ์. - ์๋์ฝ๋
async function findAndSaveUser(Users) {
try {
let user = await Users.findOne({});
user.name = 'zero';
user = await user.save();
user = await Users.findOne({ gender: 'm' });
// ์๋ต
} catch (error) {
console.error(error);
}
}
์ด๋ค ํ๋ก๋ฏธ์ค๊ฐ reject๋ ๊ฒฝ์ฐ.
์๋๋ ํ์ดํ ํจ์๋ async์ ๊ฐ์ด ์ฌ์ฉํ๋ ์์ .
const findAndSaveUser = async (Users) => {
try {
let user = await Users.findOne({});
user.name = 'zero';
user = await user.save();
user = await Users.findOne({ gender: 'm' });
// ์๋ต
} catch (error) {
console.error(error);
}
};
for๋ฌธ๊ณผ async/await์ ํตํด์ ํ๋ก๋ฏธ์ค ํ๋์ฉ ์์ฐจ์ฒ๋ฆฌ ๊ฐ๋ฅ
ES2018๋ถํฐ ๊ฐ๋ฅ
const promise1 = Promise.resolve('์ฑ๊ณต1');
const promise2 = Promise.resolve('์ฑ๊ณต2');
(async () => {
for await (promise of [promise1, promise2]) {
console.log(promise);
}
})();
๋ญ๊ฐ ์ค์ํํธ๊ฐ ์์ค๋ฅผ ๋ง์ด ๋ฐ๋ผ๊ฐ๋ค๋ ๊ธฐ๋ถ์ด๋ค.
async ํจ์์ ๋ฐํ๊ฐ์ ํญ์ Promise๋ก ๊ฐ์ธ์ง๋ค. ๋ฐ๋ผ์ ์คํ ํ then์ ๋ถ์ด๊ฑฐ๋ ๋ ๋ค๋ฅธ async ํจ์ ์์์ await์ ๋ถ์ฌ์ ์ฒ๋ฆฌํ ์ ์๋ค.
async function findAndSaveUser(Users) {
// ์๋ต
}
findAndSaveUser().then(() => { /* ์๋ต */ });
// ๋๋
async function other() {
const result = await findAndSaveUser();
}
# Map/Set
๋งต์ ๊ฐ์ฒด์ ์ ์ฌ Set์ ๋ฐฐ์ด๊ณผ ์ ์ฌ
๋งต์ C++์์ ๋ณธ๊ฑฐ๊ฐ๊ณ Set์ ์ค์ํํธ์์๋ ์๋ณด์.
- Map
์์ฑ๋ค ๊ฐ์ ์์๋ฅผ ๋ณด์ฅํ๊ณ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋ฌธ์์ด์ด ์๋ ๊ฐ๋ ์ฌ์ฉํ ์ ์๊ณ , size๋ ์ฝ๊ฒ ์ฐพ์ ์ ์์.
(๊ฐ์ธ์๊ฒฌ) ๊ทธ๋ฅ ์๋ฃ๊ตฌ์กฐ๋ผ๊ณ ์๊ฐ
const m = new Map();
m.set('a', 'b'); // set(ํค, ๊ฐ)์ผ๋ก Map์ ์์ฑ ์ถ๊ฐ
m.set(3, 'C'); // ๋ฌธ์์ด์ด ์๋ ๊ฐ์ ํค๋ก ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค
const d = {};
m.set(d, 'e'); // ๊ฐ์ฒด๋ ๋ฉ๋๋ค
m.get(a); // get(ํค)๋ก ์์ฑ๊ฐ ์กฐํ
console.log(m.get(d)); // e
m.size; // size๋ก ์์ฑ ๊ฐ์ ์กฐํ
console.log(m. size); // 3
for (const [k, v] of m) { // ๋ฐ๋ณต๋ฌธ์ ๋ฐ๋ก ๋ฃ์ด ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค
console.log (k, v); // 'a', 'b', 3, 'C', 0, 'e'
} // ์์ฑ ๊ฐ์ ์์๋ ๋ณด์ฅ๋ฉ๋๋ค
m.forEach((v, k) => { // forEach๋ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค
console.log(k, V); // ๊ฒฐ๊ณผ๋ ์์ ๋์ผ
});
m.has(d); // has(ํค)๋ก ์์ฑ ์กด์ฌ ์ฌ๋ถ ํ์ธ
console.log(d); // true
m.delete(d); // delete(ํค)๋ก ์์ฑ์ ์ญ์
m.clear(); // clear()๋ก ์ ๋ถ ์ ๊ฑฐ
console.log(m.size); // 0
- Set
Set์ ์ค๋ณต์ ํ์ฉํ์ง ์์.
const arr = [1, 3, 2, 7, 2, 6, 3, 5];
const s = new Set(arr); // ์ค๋ณต์ ๊ฑฐ
const result = Array.from(s); // ๋ค์ set์ ๋ฐฐ์ด๋ก ๋๋๋ฆฌ๊ธฐ
console.log(result); // 1, 3, 2, 7, 6, 5
# ๋ ๋ณํฉ/์ต์ ๋ ์ฒด์ด๋
ES2020์์ ๋์ด
falsy๊ฐ: (0, '', false, NaN, null, undefined)
๋ ๋ณํฉ ์ฐ์ฐ์(||) ๋ falsy๊ฐ์ค null๊ณผ undefined๋ง ๋ฐ๋ก ๊ตฌ๋ถ.
const a = 0;
const b = a || 3; // || ์ฐ์ฐ์๋ falsy ๊ฐ์ด๋ฉด ๋ค๋ก ๋์ด๊ฐ
console.log(b); // 3
const c = 0;
const d = c ?? 3; // ?? ์ฐ์ฐ์๋ null์ผ๋๋ undetined์ผ ๋๋ง ๋ค๋ก ๋์ด๊ฐ
console.log(d); // 0;
const e = null;
const f = e ?? 3;
console.log(f); // 3;
const g = undefined;
const h = g ?? 3;
console.log(h); // 3;
์ต์ ๋ ์ฒด์ด๋
์๋ ์์๋ฅผ ๋ณด์. ๊ทผ๋ฐ ์ด๊ฑด ๋ญ ์ค์ํํธ์์๋ ์ดํดํ๋ ๊ฐ๋ ์ด๋ฏ๋ก ๋น์ฐํ๋ค ์ถ์
{}๋ฅผ ๊ฐ์ฒด๋ก ํํํ๋๋ฐ๋ง ์ฃผ๋ชฉํ์.
const a = {}
a.b: // a๊ฐ ๊ฐ์ฒด์ด๋ฏ๋ก ๋ฌธ์ ์์
const c = null;
try {
c.d;
} catch (e) {
console.error(e); // TypeError: Cannot read properties of null (reading 'd')
}
c.d?; // ๋ฌธ์ ์์
try {
c.f();
} catch (e) {
console. error(e); // TypeError: Cannot read properties of null (reading 'f')
๏ฝ
c?.f(); ๋ฌธ์ ์์
try {
c[0];
} catch (e) {
console. error(e); // TypeError: Cannot read properties of null (reading '0')
}
# AJAX
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
// ์ฌ๊ธฐ์ ์์ ์ฝ๋๋ฅผ ๋ฃ์ผ์ธ์.
</script>
get ์์ฒญ ๋ณด๋ด๋ณด๊ธฐ
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios.get('https://www.zerocho.com/api/get')
.then((result) => {
console.log(result);
console.log(result.data); // {}
})
.catch((error) => {
console.error(error);
});
</script>
async await ์ฌ์ฉ
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
(async () => {
try {
const result = await axios.get('https://www.zerocho.com/api/get');
console.log(result);
console.log(result.data); // {}
} catch (error) {
console.error(error);
}
})();
</script>
์ด๋ฒ์๋ post ์ฐ์ต
- ์ ์ฒด์ ์ธ ๊ตฌ์กฐ๋ ๋น์ทํ๋ ๋ฐ๋ ๋ถ๋ถ์ด ์ถ๊ฐ๋์์.
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
(async () => {
try {
const result = await axios.post('https://www.zerocho.com/api/post/json', {
name: 'zerocho',
birth: 1994,
});
console.log(result);
console.log(result.data);
} catch (error) {
console.error(error);
}
})();
</script>
# FormData
์ด๊ฒ ๋ญ๋๋ฉด HTML ํ๊ทธ์ form ํ๊ทธ์ ๋ฐ์ดํฐ๋ฅผ ๋์ ์ผ๋ก ์ ์ดํ ์ ์๋ ๊ธฐ๋ฅ.
์ฃผ๋ก AJAX์ ํจ๊ป ์ฌ์ฉ๋๋ค.
๋จผ์ FormData ์์ฑ์๋ก formdata ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
๋ค์ ์ฝ๋๋ฅผ ํ ์ค์ฉ console ํญ์ ์ ๋ ฅ.
<script>
const formData = new FormData();
formData.append('name', 'zerocho');
formData.append('item', 'orange');
formData.append('item', 'melon');
formData.has('item'); // true
formData.has('money'); // false;
formData.get('item');// orange
formData.getAll('item'); // ['orange', 'melon'];
formData.append('test', ['hi', 'zero']);
formData.get('test'); // hi, zero
formData.delete('test');
formData.get('test'); // null
formData.set('item', 'apple');
formData.getAll('item'); // ['apple'];
</script>
์์ฑ๋ ๊ฐ์ฒด์ append๋ฅผ ํตํด ํค-๋ฐธ๋ฅ๋ก ๋ฐ์ดํฐ ์ ์ฅ ๊ฐ๋ฅ.
์ด์ ํผ ๋ฐ์ดํฐ๋ฅผ axios๋ก ์๋ฒ๋ก ๋ณด๋ด๋ฉด๋๋ค.
๊ทธ๋ฅ ๋ฐ๋๋ฅผ ๋ ๊น๋ํ๊ฒ ์ฒ๋ฆฌํ ๋๋.
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
(async () => {
try {
const formData = new FormData();
formData.append('name', 'zerocho');
formData.append('birth', 1994);
const result = await axios.post('https://www.zerocho.com/api/post/formdata', formData);
console.log(result);
console.log(result.data);
} catch (error) {
console.error(error);
}
})();
</script>
# encodeURIComponent, decodeURIComponent
AJAX์ ์์ฒญ์ ๋ณด๋ผ ๋ ์ฃผ์์ ํ๊ธ์ด ๋ค์ด๊ฐ ๊ฒฝ์ฐ ์๋ฒ์ ๋ฐ๋ผ ํ๊ธ ์ฃผ์๋ฅผ ์ดํดํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ๊ฐ ์์ด์ window๊ฐ์ฒด์ ๋ฉ์๋์ธ encodeURIComponent ๋ฉ์๋๋ฅผ ์ฌ์ฉ
ํด๋น ๋ฉ์๋๋ ๋ ธ๋์์ ์ฌ์ฉ ๊ฐ๋ฅ
ํ๊ธ ๋ถ๋ถ๋ง ๊ฐ์ธ๊ธฐ.
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
(async () => {
try {
const result = await axios.get(`https://www.zerocho.com/api/search/${encodeURIComponent('๋
ธ๋')}`);
console.log(result);
console.log(result.data); // {}
} catch (error) {
console.error(error);
}
})();
</script>
๋ฐ๋์ชฝ๋ ์ธ์ฝ๋ฉ๋์ด ๋ฐ๋๋ฐ
decodeURIComnponent('%EB....'); // ๋
ธ๋
๋ฐ๋์ชฝ์์ ๋์ฝ๋ ์ฒ๋ฆฌ๊ฐํ์ํ๋ค.
# ๋ฐ์ดํฐ ์์ฑ๊ณผ dataset
๋ ธ๋๋ฅผ ์น ์๋ฒ๋ก ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ํด๋ผ์ด์ธํธ์ ๋น๋ฒํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๊ฒ ๋๋ค.
์ด๋ ์๋ฒ์์ ๋ณด๋ด์ค ๋ฐ์ดํฐ๋ฅผ ํ๋ฐํธ์๋ ์ด๋์ ๋ฃ์ด์ผ ํ ์ง ๊ณ ๋ฏผ.
๊ฐ์ฅ ๋จผ์ ๊ณ ๋ฏผํด์ผํ ์ ์ ๋ณด์!
๋ฏผ๊ฐํ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ๋ณด๋ด์ง๋ง๊ณ , ๋น๋ฐ๋ฒํธ ๊ฐ์๊ฒ๋ ๋ด๋ ค๋ณด๋ด์ง ๋ง๊ธฐ.
๋ณด์๊ณผ ๊ด๋ จ์ด ์๋ ๋ฐ์ดํฐ๋ ์์ ๋กญ๊ฒ ํ๋ฐํธํธ์๋๋ก ๋ณด๋ด๋๋๋ค.
<ul>
<li data-id="1" data-user-job="programmer">Zero</li>
<li data-id="2" data-user-job="designer">Nero</li>
<li data-id="3" data-user-job="programmer">Hero</li>
<li data-id="4" data-user-job="ceo">Kero</li>
</ul>
<script>
console.log(document.querySelector('li').dataset);
// { id: '1', userJob: 'programmer' }
</script>
์์๊ฐ์ด HTML5 ํ๊ทธ์ ์์ฑ์ผ๋ก data-๋ก ์์ํ๋ ๊ฒ๋ค์๋ฃ๋๋ค.
์ฃผ์์ ๋ณด๋ฉด ์กฐ๊ธ์ฉ ๋ณํ ๋์๋๋ฐ, data- ์ ๋์ด๋ ์ฌ๋ผ์ง๊ณ - ๋ค์ ์์นํ ๊ธ์๋ ๋๋ฌธ์๊ฐ ๋๋ค. data-id๋ id, data-user-job์ userJob์ด ๋์๋ค.
๋ฐ๋๋ก dataset์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด๋ HTML์ ๋ฐ์๋๋ค.
dataset.monthSalary = 10000;์ ๋ฃ์ผ๋ฉด data-moth-salary = "10000" ์ด๋ผ๋ ์์ฑ์ด ์๊น
'๐ฐ๏ธ Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Node.js] #6 ์ต์คํ๋ ์ค ์น ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.07.23 |
---|---|
[Node.js] #5 ํจํค์ง ๋งค๋์ (0) | 2023.07.23 |
[Node.js] #4 http ๋ชจ๋๋ก ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.07.22 |
[Node.js] #3 ๋ ธ๋ ๊ธฐ๋ฅ ์์๋ณด๊ธฐ (0) | 2023.07.22 |
[Node.js] #1 ํต์ฌ๊ฐ๋ ์ดํดํ๊ธฐ (0) | 2023.07.16 |