[221101] JS : ํ์ค๋ด์ฅ๊ฐ์ฒด 3
1. ๋ฐฐ์ด
- splice : ๋์ ๋ฐฐ์ด์ ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ๊ฑฐ๋ ๊ต์ฒดํฉ๋๋ค. ๋์ ๋ฐฐ์ด ์๋ณธ์ด ๋ณ๊ฒฝ๋ฉ๋๋ค.
- arr.splice(์์ ์ธ๋ฑ์ค, ์ญ์ ํ ์์ ๊ฐ์, ์ถ๊ฐํ ์์
- ์์ ์ถ๊ฐ : arr.splice(2๋ฒ ์ธ๋ฑ์ค, 0๊ฐ ์ญ์ , ๊ทธ์๋ฆฌ์ 'X' ์ถ๊ฐ)
const arr = ['A', 'B', 'C']
arr.splice(2, 0, 'X')
console.log(arr) // ['A', 'B', 'X', 'C']
- ์์ ์ญ์ : arr.splice(1๋ฒ ์ธ๋ฑ์ค, 1๊ฐ ์ญ์ )
const arr = ['A', 'B', 'C']
arr.splice(1, 1)
console.log(arr) // ['A', 'C']
- ์์ ๊ต์ฒด : arr.splice(1๋ฒ ์ธ๋ฑ์ค, 1๊ฐ ์ญ์ , ๊ทธ์๋ฆฌ์ 'X' ์ถ๊ฐ)
const arr = ['A', 'B', 'C']
arr.splice(1, 1, 'X')
console.log(arr) // ['A', 'X', 'C']
- ์์ ์ถ๊ฐ ๋ฐ ์ญ์ : arr.splice(0๋ฒ ์ธ๋ฑ์ค, 0๊ฐ ์ญ์ , ๊ทธ์๋ฆฌ์ 'X', 'Y', 'Z' ์ถ๊ฐ)
const arr = ['A', 'B', 'C']
arr.splice(0, 0, 'X', 'Y', 'Z')
console.log(arr) // ['X', 'Y', 'Z', 'A', 'B', 'C']
- ํ๋กํ ํ์ ๋ฉ์๋ : ๋ฐ์ดํฐ์ ๋ถ์ฌ์ ์ฐ๋ ๋ฉ์๋
- ์ ์ ๋ฉ์๋ : class ๊ฐ์ฒด์์ ๋ฐ๋ก ์ฌ์ฉํ๋ ๋ฉ์๋๋ก ๋ฐฐ์ด์๋ ์ฌ์ฉํ ์ ์์
2. Array
(1) Array.from
- ์ ์ฌ ๋ฐฐ์ด(Array-like)์ ์ค์ ๋ฐฐ์ด๋ก ๋ฐํ.
- li ํ๊ทธ ๋ชจ๋๋ฅผ querySelectorAll ๋ก ๊ฐ์ ธ์จ ์์๋ฅผ console.log ํด๋ณด๋ฉด nodelist ๊ฐ ๋์ค๋๋ฐ nodelist ๋ ์ ์ฌ๋ฐฐ์ด. ์ ์ฌ๋ฐฐ์ด์ ๊ฐ์ฒด๋ฐ์ดํฐ๋ก ๋ฐฐ์ด๋ฐ์ดํฐ๊ฐ ์๋.
- Array.from(liEls).map( ) → from ์ผ๋ก ๊ฐ์ธ์ฃผ๋ฉด ์ ์ฌ ๋ฐฐ์ด์ ์ค์ ๋ฐฐ์ด๋ก ์ฌ์ฉ๊ฐ๋ฅ.
- ๋ค๋ง, ๊ฐ key ๊ฐ์ด ์ ๋ก๋ฒ ์ด์ค ๋๋ฒ๋ง์ด ๋์ด์๊ณ ๋ง์ง๋ง์๋ length ์ ๊ทธ ๊ฐ์ด ์์ด์ผ ํจ.
(2) Array.isArray
- ๋ฐฐ์ด ๋ฐ์ดํฐ์ธ์ง ํ์ธ.
- ์ค๊ดํธ ์์ ๋ฐ์ดํฐ๊ฐ ๋ฐฐ์ด ๋ฐ์ดํฐ์ธ์ง ํ์ธํ์ฌ ๋ฐํ
3. Object
(1) Object.assign
- ํ๋ ์ด์์ ์ถ์ฒ(Source) ๊ฐ์ฒด๋ก๋ถํฐ ๋์(Target) ๊ฐ์ฒด๋ก ์์ฑ์ ๋ณต์ฌํ๊ณ ๋์ ๊ฐ์ฒด๋ฅผ ๋ฐํ.
- ๋๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ณํฉํ๋ concat ๋ฉ์๋์ ์ ์ฌ
- ์ธ์๋ ์ต์ ๋๊ฐ ์ด์์ ๋ฃ์ด์ค์ผ ํจ ํ๊ณ๋ ์์
const target = { a: 1, b: 2 }
const source1 = { b: 3, c: 4 }
const source2 = { c: 5, d: 6 }
const result = Object.assign(target, source1, source2)
console.log(target) // { a: 1, b: 3, c: 5, d: 6 }
console.log(result) // { a: 1, b: 3, c: 5, d: 6 }
→ ์๋ณธ ๋ฐ์ดํฐ์ธ target ์ด ๋ณ๊ฒฝ๋๊ณ result ์ ๋ค์ด๊ฐ, const result = target ๊ณผ ๊ฐ์ ์๋ฏธ
→ ๊ฐ์ฒด ๋ฐ์ดํฐ์์ ์์ฑ์ ์ด๋ฆ(key) ๋ ๊ณ ์ ํจ, ๋ ๊ฐ์ ์ด๋ฆ์ ์กด์ฌํ ์ ์์. ๋ฐ๋ผ์ ์์ ๋ฐ์ดํฐ๋ ๋ ๋ผ๊ฐ๊ณ ๋ค์์ ๋ฎ์ด์ด ๋ฐ์ดํฐ๋ฅผ ๋ฐํ.
- ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ค๋ฉด, ๋น ๊ฐ์ฒด๋ฅผ ๋์์ผ๋ก ์ถ๊ฐ ๊ฐ๋ฅ
const target = { a: 1, b: 2 }
const source1 = { b: 3, c: 4 }
const source2 = { c: 5, d: 6 }
const result = Object.assign({}, target, source1, source2)
console.log(target) // { a: 1, b: 2 }
console.log(result) // { a: 1, b: 3, c: 5, d: 6 }
- ์ ๊ฐ ์ฐ์ฐ์ ์ฌ์ฉ ๊ฐ๋ฅ.
const target = { a: 1, b: 2 }
const source1 = { b: 3, c: 4 }
const source2 = { c: 5, d: 6 }
const result = {
...target,
...source1,
...source2
}
console.log(target) // { a: 1, b: 2 }
console.log(result) // { a: 1, b: 3, c: 5, d: 6 }
→ ์๋ณธ ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋์ง ์์
(2) Object.keys
- ์ฃผ์ด์ง ๊ฐ์ฒด์ ์์ฑ ์ด๋ฆ์ ๋์ดํ ๋ฐฐ์ด์ ๋ฐํ.
(3) Object.values
- ์ฃผ์ด์ง ๊ฐ์ฒด์ ์์ฑ ๊ฐ์ ๋์ดํ ๋ฐฐ์ด์ ๋ฐํ.
(4) Object.entries
- ์ฃผ์ด์ง ๊ฐ์ฒด์ ๊ฐ ์์ฑ๊ณผ ๊ฐ์ผ๋ก ํ๋์ ๋ฐฐ์ด ๋ง๋ค์ด ์์๋ก ์ถ๊ฐํ 2์ฐจ์ ๋ฐฐ์ด์ ๋ฐํ.
- 2์ฐจ์ ๋ฐฐ์ด์ ๋ฐฐ์ด ์์ ๋ฐฐ์ด์ด ๋ค์ด๊ฐ ๊ฒ์ ์๋ฏธ.
- for in, for of, object.keys, object.entries ๋ฑ ๋ค์ํ ๋ฐฉ๋ฒ ์์
(5) Object.freeze ์ Object.isFrozen
- ์ฃผ์ด์ง ๊ฐ์ฒด๋ฅผ ๋ณ๊ฒฝํ ์ ์๋๋ก ๋๊ฒฐํ๊ฑฐ๋, ๋๊ฒฐ ์ฌ๋ถ๋ฅผ ํ์ธ.
(6) Object.seal ๊ณผ Object.isSealed
- ์ฃผ์ด์ง ๊ฐ์ฒด๋ฅผ ๋ณ๊ฒฝํ ์ ์๋๋ก ๋ฐ๋ดํ๊ฑฐ๋, ๋ฐ๋ด ์ฌ๋ถ๋ฅผ ํ์ธ.
- ๋๊ฒฐ๊ณผ ๋ค๋ฅธ ์ ์ ๋ฐ๋ด ํ์๋ ์์ฑ์ ๊ฐ ๋ณ๊ฒฝ ๊ฐ๋ฅ.
- ์ถ๊ฐ, ์ญ์ ๋ ์๋์ง๋ง ์์ ์ ๊ฐ๋ฅ.
(7) Object.defineProperty
- ์ฃผ์ด์ง ๊ฐ์ฒด์ ์์ฑ์ ์ถ๊ฐํ๊ฑฐ๋, ํน์ฑ์ ๋ณ๊ฒฝ.
const user = {}
Object.defineProperty(user, 'name', {
value: 'Heropy'
})
console.log(user.name) // 'Heropy'
// ์ด๊ฑฐ ๋ถ๊ฐ
for (const key in user) {
console.log('key:', key) // ์ถ๋ ฅ ์์
}
// ์์ ๋ถ๊ฐ
user.name = 'Neo'
console.log(user.name) // 'Heropy'
// ์ญ์ ๋ถ๊ฐ
delete user.name
console.log(user.name) // 'Heropy'
→ ์์ฑ์ true ๋ก ๋ฐ๋ก ์ ์ํ์ง ์์์ ๋ชจ๋ ๊ธฐ๋ณธ๊ฐ์ด false ์ด๊ธฐ ๋๋ฌธ์ ์ด๊ฑฐ, ์์ , ์ญ์ ๊ฐ ๋ถ๊ฐํ ๊ฒ
const user = {
_name: "Heropy",
};
Object.defineProperty(user, "name", {
get() {
return this._name;
},
set(value) {
this._name = value;
console.log(`์ด๋ฆ์ด ${value}๋ก ๋ฐ๋์์ต๋๋ค!`);
},
});
// Get!
console.log(user.name); // 'Heropy'
// Set!
user.name = "Neo"; // '์ด๋ฆ์ด Neo๋ก ๋ฐ๋์์ต๋๋ค!'
for (let key in user) {
console.log(key); // '_name'
}
→ _ ๋ ์ธ์ ์์ด์ ธ๋ ์ด์ํ์ง ์์ ๋ฐ์ดํฐ์ด๊ธฐ ๋๋ฌธ์ ์กฐํ๋ ๊ฐ๋ฅํ์ง๋ง ์๋ฌต์ ์ผ๋ก ์ฐ์ง๋ง๋ผ๊ณ ์ง์ ํด๋์ ๊ฒ.
→ this : user
→ get : ์กฐํํ ๋ ๋์ํ๋ ํจ์
→ set : ์ง์ ํ ๋ ๋์ํ๋ ํจ์. ์ด ํจ์๋ ๋ฐ์ดํฐ๋ฅผ ์ง์ ํ ๋ ๋์ํ๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ์ value ๋ 'Neo'
(8) Object.defineProperties
- ์ฃผ์ด์ง ๊ฐ์ฒด์ ์ฌ๋ฌ ์์ฑ์ ์ถ๊ฐํ๊ฑฐ๋, ํน์ฑ์ ๋ณ๊ฒฝ.
- ์ฌ๋ฌ ๊ฐ์ ์์ฑ์ ํ๊บผ๋ฒ์ ์ง์ ํด์ค ์ ์์
const user = {};
Object.defineProperties(user, {
name: {
enumerable: true,
value: "Heropy",
},
age: {
enumerable: true,
value: 85,
},
email: {
enumerable: true,
configurable: true,
writable: true,
value: [],
},
address: {
value: "๊ฒฝ๊ธฐ ์์์",
},
});
console.log(user);
for (const key in user) {
console.log(key);
}
// 'name'
// 'age'
// 'email'
4. JSON
- JSON(JavaScript Object Notation)๋ ๋ฐ์ดํฐ ์ ๋ฌ์ ์ํ ํ์ค ๋ฐ์ดํฐ ํฌ๋งท.
- ๋ฌธ์, ์ซ์, ๋ถ๋ฆฐ, Null, ๊ฐ์ฒด, ๋ฐฐ์ด๋ง ์ฌ์ฉ
- ๋ฌธ์๋ ํฐ ๋ฐ์ดํ๋ง ์ฌ์ฉ
- ํํ ์ผํ ์ฌ์ฉ ๋ถ๊ฐ → ๋ค์ ๋ฐ์ดํฐ๊ฐ ์๋๋ฐ๋ ๋ถ๊ตฌํ๊ณ ๋์ ์ผํ๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ฌ
- .json ํ์ฅ์ ์ฌ์ฉ
(1) stringify
- JavaScript ๋ฐ์ดํฐ๋ฅผ JSON ๋ฌธ์๋ก ๋ณํ.
console.log(JSON.stringify("Hello world!")); // '"Hello world!"'
console.log(JSON.stringify(123)); // '123'
console.log(JSON.stringify(false)); // 'false'
console.log(JSON.stringify(null)); // 'null'
console.log(JSON.stringify({ name: "Heropy", age: 85 })); // '{"name":"Heropy","age":85}'
// console.log(JSON.stringify({ name: 'Heropy', age: 85 }, null, 2))
console.log(JSON.stringify([1, 2, 3])); // '[1,2,3]'
(2) parse
- JSON ๋ฌธ์๋ฅผ ๋ถ์ํด JavaScript ๋ฐ์ดํฐ๋ก ๋ณํ.
console.log(JSON.parse('"Hello world!"')); // "Hello world!"
console.log(JSON.parse("123")); // 123
console.log(JSON.parse("false")); // false
console.log(JSON.parse("null")); // null
console.log(JSON.parse('{"name":"Heropy","age":85}')); // { name: 'Heropy', age: 85 }
console.log(JSON.parse("[1,2,3]")); // [1, 2, 3]
5. ๋ชจ๋
- ๋ชจ๋(Module)์ ์ดํด ๊ฐ๋ฅํ, ๋ณด๋ค ์์ ๋จ์๋ก ๋๋ ์ง ๊ฒ์ ๋งํฉ๋๋ค.
- ํนํ ์๋ฐ์คํฌ๋ฆฝํธ์์ ๋ชจ๋์ ํน์ ๋ฐ์ดํฐ๋ค์ ์งํฉ(ํ์ผ)์ ๋๋ค.
(1) ๋ด๋ณด๋ด๊ธฐ(export)
- ๊ธฐ๋ณธ ๋ด๋ณด๋ด๊ธฐ(Default exports) : ์ด๋ฆ X (์ ํ), ๋ชจ๋๋น 1๋ฒ๋ง ์ฌ์ฉ ๊ฐ๋ฅ
export default ๋ฐ์ดํฐ;
- ์ด๋ฆ ๋ด๋ณด๋ด๊ธฐ(Named exports) : ์ด๋ฆ ํ์, ๋ชจ๋๋น n๋ฒ ์ฌ์ฉ ๊ฐ๋ฅ
export const ์ด๋ฆ1 = ๋ฐ์ดํฐ1;
export const ์ด๋ฆ2 = ๋ฐ์ดํฐ2;
- ์ฌ์ฉ ํจํด
export default {
name: "๊ธฐ๋ณธ ๋ฐ์ดํฐ!",
desc: "์ด๋ฆ ํ์ ์์!",
};
export const str = "Hello~";
export const num = 123;
export const arr = ["A", "B", "C"];
export function hello() {}
- as ํค์๋ : ๋ด๋ณด๋ด๋ ๋ฐ์ดํฐ์ ์ด๋ฆ ๋ณ๊ฒฝ
const a = "Named!";
const b = 123;
const c = ["A", "B", "C"];
function d() {}
export { a as str, b as num, c as arr, d as hello };
(2) ๊ฐ์ ธ์ค๊ธฐ(Imports)
- ๊ตฌ์กฐ : import ๊ธฐ๋ณธ๋ฐ์ดํฐ, { ์ด๋ฆ๋ฐ์ดํฐ1, ์ด๋ฆ๋ฐ์ดํฐ2 } from '๊ฒฝ๋ก'
import defData from "./myModule.js";
import defData, { str, num, arr, fn } from "./myModule.js";
import { str, num, arr, fn } from "./myModule.js";
import {
str as myStr,
num as myNum,
arr as myArr,
fn as myFn,
} from "./myModule.js";
import * as myName from "./myModule.js";
→ *๋ ์์ด๋์นด๋ ๋ฌธ์(wildcard character)๋ก ์ฌ๋ฌ ๊ฐ๋ฅผ ํ ๋ฒ์ ์ง์ ํ๋ค๋ ์๋ฏธ์ ๊ธฐํธ