๐ก ๋์ ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํต์ฌ ๊ฐ์ด๋๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํฉ๋๋ค.
10.1 ๋ณ์๋ฅผ ํค์ ๊ฐ์ผ๋ก ํ๋ ๊ฐ์ฒด ๋ง๋ค๊ธฐ
๊ฐ์ฒด์ key์ value๊ฐ ๋์ผํ ๊ฒฝ์ฐ, value ๊ฐ์ ์๋ตํ์ฌ ํํ ๊ฐ๋ฅ
const name = "Alberto";
const surname = "Montalesi";
const age = 25;
const nationality = "Italian";
const person = {
name,
surname,
age,
nationality,
};
console.log(person);
// {name: "Alberto", surname: "Montalesi", age: 25, nationality: "Italian"}
10.2 ๊ฐ์ฒด์ ํจ์ ์ถ๊ฐํ๊ธฐ
์ผ๋ฐ ํจ์
// ES5
const person = {
name: "Alberto",
greet: function () {
console.log("Hello");
},
};
// ES6
const person = {
name: "Alberto",
greet() {
console.log("Hello");
},
};
person.greet(); // hello
ํ์ดํ ํจ์
Arrow function์ ์ต๋ช ํจ์์ด๋ฏ๋ก ํจ์์ ์ ๊ทผํ๊ธฐ ์ํ ํค๊ฐ ํ์ํจ
// ์๋ชป๋ ์
const person1 = {
() => console.log("Hello"),
};
// Uncaught SyntaxError: Unexpected token '('
// ์ฌ๋ฐ๋ฅธ ์
const person2 = {
greet: () => console.log("Hello"),
}
person2.greet()
// Hello
10.3 ๊ฐ์ฒด์ ์์ฑ์ ๋์ ์ผ๋ก ์ ์ํ๊ธฐ
// ES5
var name = "myname";
// create empty object
var person = {};
// update the object
person[name] = "Alberto";
console.log(person.myname);
// Alberto
// ES6
const name = "myname";
const person = {
[name]: "Alberto",
};
console.log(person.myname);
// Alberto
'Tech > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Chapter 12: Classes (0) | 2022.01.26 |
---|---|
Chapter 11: ์๋ฐ์คํฌ๋ฆฝํธ ์ฌ๋ณผ (Symbols) (0) | 2022.01.25 |
Chapter 09: Spread์ Rest (Spread operator and rest parameters) (0) | 2022.01.23 |
Chapter 08: ๋ฐฐ์ด ๋ฉ์๋ (Array improvements) (0) | 2022.01.22 |
Chapter 07: ๋ฐ๋ณต๋ฌธ (Loop) (0) | 2022.01.20 |