
[JS] ๊ฐ์ฒด
๊ฐ์ฒด
JavaScript์์๋ ์์ ๊ฐ์ ์ ์ธํ ๋ชจ๋ ๊ฒ์ด ๊ฐ์ฒด์ ๋๋ค.
์์ ๊ฐ | ๊ฐ์ฒด |
---|---|
๋ณ๊ฒฝ ๋ถ๊ฐํ ๊ฐ | ๋ณ๊ฒฝ ๊ฐ๋ฅํ ๊ฐ |
๋ฉ์๋๋ฅผ ๊ฐ์ง ์ ์์ | ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ ์์ |
๊ฐ์ ์ํ ์ ๋ฌ | ์ฐธ์กฐ์ ์ํ ์ ๋ฌ |
๊ฐ์ฒด๋ ํ๋กํผํฐ๋ก ๊ตฌ์ฑ๋๋ฉฐ, ํ๋กํผํฐ๋ Key์ Value๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
Key๋ ์ค๋ณต ์ ์ธ๋ ์ ์์ผ๋ฉฐ, ์ด ๊ฒฝ์ฐ ๋ง์ง๋ง ์ ์ธ๋ ๊ฐ์ด ์ ์ฅ๋ฉ๋๋ค.
ํ๋กํ ํ์ ๊ธฐ๋ฐ ์ธ์ด
C++์ด๋ Java์ ๊ฐ์ ์ธ์ด๋ ํด๋์ค ๊ธฐ๋ฐ ์ธ์ด์ด๋ฉฐ, ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
๋ฐ๋ฉด JavaScript๋ ํ๋กํ ํ์
๊ธฐ๋ฐ ์ธ์ด์
๋๋ค.
๊ฐ์ฒด๋ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์์ผ๋ฉฐ, ์ด๋ฅผ ํ๋กํ ํ์ ์ด๋ผ๊ณ ํฉ๋๋ค.
๊ฐ์ฒด๋ ํ๋กํ ํ์ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์์๋ฐ์ ์ ์์ต๋๋ค.
ํ์ ํ Object.create
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ๋กํ ํ์
๊ฐ์ฒด๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
const person = {
name: 'John Doe',
age: 30,
address: '123 Main Street',
greet: function() {
console.log('Hello, my name is ' + this.name);
}
};
const student = Object.create(person);
student.name = 'Jane Doe';
student.grade = 'A';
์ด ์์ ์ ๊ฒฝ์ฐ student
๊ฐ์ฒด๋ person
๊ฐ์ฒด๋ฅผ ํ๋กํ ํ์
์ผ๋ก ์ง์ ํ์์ผ๋ฏ๋ก
person
๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์์๋ฐ์ ์ ์์ต๋๋ค.
ํ๋กํผํฐ ์ ๊ทผ
์ ํ๊ธฐ๋ฒ
์ ํ๊ธฐ๋ฒ์ ์ฌ์ฉํ์ฌ ํ๋กํผํฐ์ ์ ๊ทผํ ์ ์์ต๋๋ค.
const person = {
name: 'John Doe',
age: 30,
address: '123 Main Street',
greet: function() {
console.log('Hello, my name is ' + this.name);
}
};
console.log(person.name); // John Doe
๋๊ดํธ ํ๊ธฐ๋ฒ
์ด์ธ์๋ ๋๊ดํธ ํ๊ธฐ๋ฒ์ ์ฌ์ฉํ์ฌ ํ๋กํผํฐ์ ์ ๊ทผํ ์ ์์ต๋๋ค.
const person = {
name: 'John Doe',
age: 30,
address: '123 Main Street',
greet: function() {
console.log('Hello, my name is ' + this.name);
}
};
console.log(person['name']); // John Doe
๋๊ดํธ ํ๊ธฐ๋ฒ์ ์ฌ์ฉํ ๋๋ Key๋ฅผ ๋ฌธ์์ด๋ก ์์ฑํด์ผ ํฉ๋๋ค.
ํ๋กํผํฐ ์ญ์
delete
์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ํ๋กํผํฐ๋ฅผ ์ญ์ ํ ์ ์์ต๋๋ค.
const person = {
name: 'John Doe',
age: 30,
address: '123 Main Street',
greet: function() {
console.log('Hello, my name is ' + this.name);
}
};
delete person.age;
ํ๋กํผํฐ ์กด์ฌ ํ์ธ
in
์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ํ๋กํผํฐ์ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
const person = {
name: 'John Doe',
age: 30,
address: '123 Main Street',
greet: function() {
console.log('Hello, my name is ' + this.name);
}
};
console.log('name' in person); // true
console.log('age' in person); // true
console.log('email' in person); // false
ํ๋กํผํฐ ์ด๊ฑฐ
for...in
๋ฌธ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ด๊ฑฐํ ์ ์์ต๋๋ค.
const person = {
name: 'John Doe',
age: 30,
address: '123 Main Street',
greet: function() {
console.log('Hello, my name is ' + this.name);
}
};
for (const key in person) {
console.log(key + ': ' + person[key]);
}
ํ๋กํผํฐ ๋์ ์์ฑ
๊ฐ์ฒด๋ ๋์ ์ผ๋ก ํ๋กํผํฐ๋ฅผ ์์ฑํ์ฌ ์ถ๊ฐํ ์ ์์ต๋๋ค.
const person = {};
person.name = 'John Doe';
person.age = 30;
person.address = '123 Main Street';
person.greet = function() {
console.log('Hello, my name is ' + this.name);
};
๊ฐ์ฒด ์์ฑํ๊ธฐ
๊ฐ์ฒด ๋ฆฌํฐ๋ด
์ค๊ดํธ {}
๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์
๋๋ค.
const person = {
name: 'John Doe',
age: 30,
address: '123 Main Street',
greet: function() {
console.log('Hello, my name is ' + this.name);
}
};
๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋๋ new
ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉฐ
Key: Value
ํํ๋ก ํ๋กํผํฐ๋ฅผ ์์ฑํฉ๋๋ค.
Object ์์ฑ์ ํจ์
Object
์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
const person = new Object();
person.name = 'John Doe';
person.age = 30;
person.address = '123 Main Street';
person.greet = function() {
console.log('Hello, my name is ' + this.name);
};
์์ฑ์ ํจ์
์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
function Person(name, age, address) {
this.name = name;
this.age = age;
this.address = address;
this.greet = function() {
console.log('Hello, my name is ' + this.name);
};
}
const person = new Person('John Doe', 30, '123 Main Street');
์์ฑ์ ํจ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํจ๊ณผ ๋์์ ์ด๊ธฐํ๋ฅผ ์ํํ ์ ์์ต๋๋ค.
Object.create ๋ฉ์๋
Object.create
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
const person = Object.create({
name: 'John Doe',
age: 30,
address: '123 Main Street',
greet: function() {
console.log('Hello, my name is ' + this.name);
}
});
Object.create
๋ฉ์๋๋ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ํ๋กํ ํ์
๊ฐ์ฒด๋ฅผ ์ ๋ฌ๋ฐ๊ณ
๋ ๋ฒ์งธ ์ธ์๋ก ํ๋กํผํฐ๋ฅผ ๋ฐ์ ์ ์์ต๋๋ค.
const person = Object.create({
greet: function() {
console.log('Hello, my name is ' + this.name);
}
}, {
name: {
value: 'John Doe',
writable: true,
enumerable: true,
configurable: true
},
age: {
value: 30,
writable: true,
enumerable: true,
configurable: true
},
address: {
value: '123 Main Street',
writable: true,
enumerable: true,
configurable: true
}
});
์์ธํ ๋ด์ฉ์ MDN์ ์ฐธ๊ณ ํ์ธ์.
ํด๋์ค
ES6์์ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
class Person {
constructor(name, age, address) {
this.name = name;
this.age = age;
this.address = address;
}
greet() {
console.log('Hello, my name is ' + this.name);
}
}
const person = new Person('John Doe', 30, '123 Main Street');
JavaScript๋ ํ๋กํ ํ์ ๊ธฐ๋ฐ ์ธ์ด์ด๋ฏ๋ก ํด๋์ค๋ ์ฌ์ค ํจ์์ ๋๋ค.
class
ํค์๋๋ ํจ์๋ฅผ ์์ฑํ๊ณ , constructor
๋ฉ์๋๋ ์์ฑ์ ํจ์๋ฅผ ์ ์ํฉ๋๋ค.