๐โโ๏ธ ์๋ฐ ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด(Object)๋,
ํค(key)๊ณผ ๊ฐ(value)์ผ๋ก ๊ตฌ์ฑ๋ ํ๋กํผํฐ(Property)๋ค์ ์งํฉ.
- ํ๋กํผํฐ์ ๊ฐ์ผ๋ก ์๋ฐ์คํฌ๋ฆฝํธ์์ ์ฌ์ฉํ ์ ์๋ ๋ชจ๋ ๊ฐ์ ์ฌ์ฉ ๊ฐ๋ฅ
- ์๋ฐ์คํฌ๋ฆฝํธ์ ํจ์๋ ์ผ๊ธ ๊ฐ์ฒด์ด๋ฏ๋ก ๊ฐ์ผ๋ก ์ทจ๊ธ ๊ฐ๋ฅ
var ๊ฐ์ฒด๋ช
= {};
var student = {
// ํ๋กํผํฐ(์์ฑ) : ๊ฐ์ฒด์ ์ํ๋ฅผ ๋ํ๋ด๋ ๊ฐ(data)์ผ๋ก ํค-๊ฐ ์์ผ๋ก ๊ตฌ์ฑ
name : '์ ๊ด์',
age : 16,
// ๋ฉ์๋ : ํ๋กํผํฐ(์ํ ๋ฐ์ดํฐ)๋ฅผ ์ฐธ์กฐํ๊ณ ์กฐ์ํ ์ ์๋ ๋์(behavior)
getinfo : function() {
return `${this.name}(์)๋ ${this.age}์ธ ์
๋๋ค.`
}
};
console.log(typeof student); // object
console.log(student);
var student2 = {}; // ํ๋กํผํฐ๋ฅผ ์ ์ํ์ง ์์ ์ ๋น ๊ฐ์ฒด ์์ฑ
console.log(typeof student2);
console.log(student2); // {} ์ถ๋ ฅ
๐โโ๏ธ ํ๋กํผํฐ(property)๋,
๊ฐ์ฒด์ ์ํ๋ฅผ ๋ํ๋ด๋ ๊ฐ(data)์ผ๋ก ํค-๊ฐ ์์ผ๋ก ๊ตฌ์ฑ
๐โโ๏ธ ํ๋กํผํฐ ํค(property key)๋,
๋น ๋ฌธ์์ด์ ํฌํจํ๋ ๋ชจ๋ ๋ฌธ์์ด ๋๋ ์ฌ๋ณผ ๊ฐ
- ๋ฌธ์์ด์ด๋ฏ๋ก ๋ฐ์ดํ๋ฅผ ์ฌ์ฉํ์ง๋ง ์๋ณ์ ๋ค์ด๋ฐ ๊ท์น์ ๋ฐ๋ฅด๋ ๊ฒฝ์ฐ ์ฌ์ฉํ์ง ์์๋ ๋จ
- ์๋ณ์ ๋ค์ด๋ฐ ๊ท์น์ ๋ฐ๋ฅด์ง ์์ ๋๋ ๋ฐ์ดํ๋ฅผ ๋ฐ๋์ ์ฌ์ฉ (ํ์ง๋ง ๊ท์น์ ๋ฐ๋ฅผ ๊ฒ์ ๊ถ์ฅ)
- ์๋ฐ์คํฌ๋ฆฝํธ์์ ์ฌ์ฉํ ์ ์๋ ๋ชจ๋ ๊ฐ์ด ํ๋กํผํฐ ๊ฐ์ด ๋ ์ ์์
var obj = {
normal : 'normal value', // normal์ ๋ฐ์ดํ๋ก ๊ฐ์ธ์ง ์์๋ ์๋์ ์ผ๋ก ๋ฌธ์์ด๋ก ์ธ์
'@ s p a c e @' : 'space value', // ํน์๋ฌธ์๋ ๊ณต๋ฐฑ์ด ์์ ๋๋ ๋ฐ์ดํ๋ก ๊ฐ์ธ์ค์ผํจ
'' : '', // ๋น ๋ฌธ์์ด ํค๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง๋ ์์ง๋ง ๊ถ์ฅ X
0 : 1, // ์ซ์ ํค๋ ๋ด๋ถ์ ์ผ๋ก ๋ฌธ์์ด๋ก ๋ณํ
var : 'var', // ์์ฝ์ด ํค๋ ์ค์ ๊ฐ ๋ฐ์ํ์ง๋ ์์ง๋ง ๊ถ์ฅ X
normal : 'new value' // ์ด๋ฏธ ์กด์ฌํ๋ ํค๋ฅผ ์ค๋ณต ์ ์ธํ๋ฉด ํ์์ ํ๋กํผํฐ๋ก ๋ฎ์ด์
};
console.log(obj);
๐โโ๏ธ ์๋ฐ์คํฌ๋ฆฝํธ์์์ ๋ฉ์๋๋,
๊ฐ์ฒด ๋ด๋ถ์ ์ ์๋ ํจ์
var dog = {
name : '์น์ฆ',
// function(food) {} : ๋ฉ์๋
eat : function(food) {
console.log(`${this.name}๋ ${food}๋ฅผ ์์ฒญ ์ข์ํด์.`)
}
};
dog.eat('๊ณ ๊ตฌ๋ง'); // ํ๋กํผํฐ ์ ๊ทผ๋ฒ ์ค ํ๋ (๋ง์นจํ ์ ๊ทผ๋ฒ)
var dog = {
name : '์น์ฆ',
// ๋ฉ์๋ : ๊ฐ์ฒด ๋ด๋ถ์ ์ ์๋ ํจ์
eat : function(food) {
console.log(`${this.name}๋ ${food}๋ฅผ ์์ฒญ ์ข์ํด์.`)
}
};
๊ฐ์ฒด๋ช
.ํ๋กํผํฐํค(์ธ์)
console.log(dog.name);
dog.eat('๊ผฌ๊ตฌ๋ฏธ');
๊ฐ์ฒด๋ช
['ํ๋กํผํฐํค'](์ธ์)
console.log(dog['name']); // ๋๊ดํธ ์์ ํ๋กํผํฐ ํค๋ฅผ ๋ฐ๋์ '๋ฌธ์์ด'๋ก ์ฝ์
dog['eat']('๋ญ๋ง๋ญ์ด');
var obj = {
'dash-key' : 'dash-value',
0 : 1
};
// console.log(obj.dash-key); // ์๋ฌ
// console.log(obj.'dash-key'); // ์ปดํ์ผ ์๋ฌ
// console.log(obj[dash-key]); // ํ๋กํผํฐ ํค๋ฅผ ๋ฌธ์์ด๋ก ๋ง๋ค์ด์ผํจ
console.log(obj['dash-key']); // ์ ์ ์๋ !
// console.log(obj.0);
// console.log(obj.'0');
console.log(obj[0]); // ์ซ์์ผ ๋๋ ๋ฌธ์์ด๋ก ๋ฐ๊พธ์ง ์์๋ ์ ์์๋
console.log(obj['0']); // ์ ์์๋
var dog = {
name : '์น์ฆ'
};
์ด๋ฏธ ์กด์ฌํ๋ ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ๋ฉด ํ๋กํผํฐ ๊ฐ์ด ๊ฐฑ์
dog.name = '๊ผฌ์';
dog['name'] = '์ธ์ ๋ฏธ' // ๋๊ดํธ ํ๊ธฐ๋ฒ๋ ์ฌ์ฉ ๊ฐ๋ฅ
console.log(dog.name);
์กด์ฌํ์ง ์๋ ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ๋ฉด ํ๋กํผํฐ๊ฐ ๋์ ์ผ๋ก ์์ฑ๋์ด ์ถ๊ฐ๋๊ณ ํ๋กํผํฐ ๊ฐ์ด ํ ๋น
dog.age = 2;
dog['age'] = 2; // ๋๊ดํธ ํ๊ธฐ๋ฒ๋ ์ฌ์ฉ ๊ฐ๋ฅ
console.log(dog); // { name: '์ธ์ ๋ฏธ', age: 2 } ์ถ๋ ฅ
delete
์ฐ์ฐ์๋ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ญ์ ํ๋ฉฐ, ๋ง์ฝ ์กด์ฌํ์ง ์๋ ํ๋กํผํฐ๋ฅผ ์ญ์ ํ๋ฉด ์๋ฌ์์ด ๋ฌด์
delete dog.age;
delete dog['age']; // ์ด๋ฏธ ์ญ์ ๋ age๋ฅผ ํ๋ฒ ๋ ์ญ์ ํด๋ ์๋ฌ์์ด ๋ฌด์
console.log(dog); // { name: '์ธ์ ๋ฏธ' } ์ถ๋ ฅ
var id = 'p-00001';
var price = 30000;
var product = {
id : id,
price : price
};
console.log(product);
๐โโ๏ธ ES6์์๋ ํ๋กํผํฐ ๊ฐ์ผ๋ก ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ๋ณ์ ์ด๋ฆ๊ณผ ํ๋กํผํฐ ํค๊ฐ ๋์ผํ ์ด๋ฆ์ด๋ฉด ํ๋กํผํฐ ํค๋ฅผ ์๋ต ๊ฐ๋ฅ ๐ฅ์ค์๐ฅ
var product2 = { id, price }; // ํ๋กํผํฐ ํค๋ ๋ณ์ ์ด๋ฆ์ผ๋ก ์๋ ์์ฑ
console.log(product2);
var prefix = 'key';
var index = 0;
var obj = {};
ES5์์๋ ๊ณ์ฐ ๋ ํ๋กํผํฐ ์ด๋ฆ์ผ๋ก ํ๋กํผํฐ ํค๋ฅผ ๋์ ์์ฑํ๋ ค๋ฉด, ๊ฐ์ฒด ๋ฆฌํฐ๋ด ์ธ๋ถ์์ ๋๊ดํธ ํ๊ธฐ๋ฒ์ ์ฌ์ฉ
obj[prefix + '-' + index++] = index;
obj[prefix + '-' + index++] = index;
obj[prefix + '-' + index++] = index;
console.log(obj);
ES6์์๋ ๊ฐ์ฒด ๋ฆฌํฐ๋ด ๋ด๋ถ์์๋ ๊ณ์ฐ๋ ํ๋กํผํฐ ์ด๋ฆ์ผ๋ก ํ๋กํผํฐ ํค๋ฅผ ๋์ ์์ฑ ๊ฐ๋ฅ
var obj2 = {
[`${prefix}-${index++}`] : index,
[`${prefix}-${index++}`] : index,
[`${prefix}-${index++}`] : index
};
console.log(obj2);
ES5์์ ๋ฉ์๋๋ฅผ ์ ์ํ๋ ค๋ฉด ํ๋กํผํฐ์ ๊ฐ์ผ๋ก ํจ์๋ฅผ ํ ๋น
var dog = {
name : '์น์ฆ',
eat : function(food) {
console.log(`${this.name}๋ ${food}๋ฅผ ์์ฒญ ์ข์ํด์.`);
}
};
dog.eat('๊ณ ๊ตฌ๋ง');
ES6์์๋ ๋ฉ์๋๋ฅผ ์ ์ํ ๋ function ํค์๋๋ฅผ ์๋ตํ ์ถ์ฝ ํํ์ ์ฌ์ฉ ๊ฐ๋ฅ
var dog2 = {
name : '์น์ฆ',
eat (food) { // ': function' ์๋ต
console.log(`${this.name}๋ ${food}๋ฅผ ์์ฒญ ์ข์ํด์.`);
}
};
dog2.eat('๊ณ ๊ตฌ๋ง');
in
: ํ๋กํผํฐ ์กด์ฌ ์ฌ๋ถ ํ์ธ
var student = {
name : '์ ๊ด์',
age : 16,
test : undefined
};
ํ๋กํผํฐ ๊ฐ์ด undefined์ผ ๋ฟ ํ๋กํผํฐ๋ ์กด์ฌํ์ง๋ง ์กด์ฌํ์ง ์๋๋ค๊ณ ํ๋ณ๋ ์ ์๋ ๋ฌธ์ ๋ฐ์
console.log(student.name === undefined); // false : ์กด์ฌํจ์ ์๋ฏธ
console.log(student.height === undefined); // true : ์กด์ฌํ์ง์์์ ์๋ฏธ
console.log(student.test === undefined); // true
console.log("name" in student); // true
console.log("height" in student); // false
console.log("test" in student); // true
๐โโ๏ธ for-in ๋ฐ๋ณต๋ฌธ์ด๋,
๊ฐ์ฒด์ ๋ชจ๋ ํค๋ฅผ ์ํํ๊ธฐ ์ํ ๋ฐ๋ณต๋ฌธ
var student = {
name : '์ ๊ด์',
age : 16,
getInfo() {
return `${this.name}์ ${this.age}์ธ ์
๋๋ค.`;
}
};
console.log(student);
// key๊ฐ์ ์ํํ๋ฉฐ ์ถ๋ ฅ
for(var pk in student) {
console.log(`key : ${pk}`); // ํ๋กํผํฐ ํค ๊ฐ
console.log(`student[pk] : ${student[pk]}`); // ํ๋กํผํฐ ํค์ ํด๋นํ๋ ๊ฐ
// console.log(`student[pk] : ${student.pk}`); // ๋ง์นจํ ํ๊ธฐ๋ฒ ์ฌ์ฉ ๋ถ๊ฐ (student์์ pk๋ผ๋ ๋ฆฌํฐ๋ด์ ์ฐพ๋ ์๋ฏธ๊ฐ ๋จ)
// key : value ํ์์ผ๋ก ์ถ๋ ฅํด๋ณด๊ธฐ
console.log(`${pk} : ${student[pk]}`);
}
๐โโ๏ธ object ์์ฑ์ ํจ์๋,
new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํจ์
// ๋น ๊ฐ์ฒด ์์ฑ
const student = new Object();
// ํ๋กํผํฐ ์ถ๊ฐ
student.name = '์ ๊ด์';
student.age = 16;
console.log(student);
๐โโ๏ธ ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ์ด์ฉํ ๊ฐ์ฒด ์์ฑ ๋ฐฉ์์ ์ง๊ด์ ์ด๊ณ ๊ฐํธํ์ง๋ง, ๋จ ํ๋์ ๊ฐ์ฒด๋ง ์์ฑํ๋ฏ๋ก ๋์ผ ํ๋กํผํฐ๋ฅผ ๊ฐ๋ ๊ฐ์ฒด๋ฅผ ์ฌ๋ฌ ๊ฐ ์์ฑํด์ผ ํ๋ ๊ฒฝ์ฐ ๋งค๋ฒ ๊ฐ์ ํ๋กํผํฐ๋ฅผ ๊ธฐ์ ํ๋ฏ๋ก ๋นํจ์จ์
๐ ๋ฐ๋ผ์ ๋์ผ ํ๋กํผํฐ๋ฅผ ๊ฐ๋ ๊ฐ์ฒด๋ฅผ ์ฌ๋ฌ ๊ฐ ์์ฑ ์, ์์ฑ์ ํจ์ ์ฌ์ฉ
// ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ ์ํ ํ
ํ๋ฆฟ์ฒ๋ผ ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ๋กํผํฐ ๊ตฌ์กฐ๊ฐ ๋์ผํ ๊ฐ์ฒด ์ฌ๋ฌ ๊ฐ ์์ฑ ๊ฐ๋ฅ
function Student(name, age) {
// ์์ฑ์ ํจ์ ๋ด๋ถ์ this๋ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํด
this.name = name;
this.age = age;
this.getInfo = function() {
return `${this.name}์ ${this.age}์ธ ์
๋๋ค.`;
}
}
// ์ธ์คํด์ค์ ์์ฑ
const student4 = new Student('๋ด๋ฏธ์ ', 29);
const student5 = new Student('์ ํ๋ง', 35);
console.log(student4);
console.log(student5);
function Student(name, age) {
// 1. ์๋ฌต์ ์ผ๋ก ์ธ์คํด์ค๊ฐ ์์ฑ๋๊ณ this์ ๋ฐ์ธ๋ฉ ๋๋ ๊ณผ์ ์ด ๋ฐํ์ ์ด์ ์ ์คํ
console.log(this); // Student {} ์ถ๋ ฅ
// 2. this์ ๋ฐ์ธ๋ฉ ๋์ด์๋ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํ
this.name = name;
this.age = age;
this.getInfo = function() {
return `${this.name}์ ${this.age}์ธ ์
๋๋ค.`;
}
// 3. ์์ฑ ๋ ์ธ์คํด์ค๊ฐ ๋ฐ์ธ๋ฉ ๋ this๊ฐ ์๋ฌต์ ์ผ๋ก ๋ฐํ
// return {};
// ๋ง์ฝ ๋ช
์์ ์ผ๋ก ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฉด ์๋ฌต์ ์ธ this ๋ฐํ์ด ๋ฌด์
// return 1; // this๋ฅผ ๋ฐํ
// ํ์ง๋ง ๋ช
์์ ์ผ๋ก ์์๊ฐ์ ๋ฐํํ๋ฉด ๋ฌด์๋๊ณ this๊ฐ ๋ฐํ
// ์์ฑ์ ๋ด๋ถ์์ return์ ์๋ตํ๋ ๊ฒ์ด ๊ธฐ๋ณธ
}
const student = new Student('ํ๊ธธ๋ฅ', 20);
console.log(student);
๐โโ๏ธ ์ฒซ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๊ธฐ์ ํ์ฌ ๊ตฌ๋ณํ๋ ๊ฒ ์ด์ธ, ์ผ๋ฐ ํจ์์ ์์ฑ์ ํจ์์ ํ์์ ์ธ ์ฐจ์ด X
- ๋จ, new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ ๋ ์์ฑ์ ํจ์๋ก ๋์ํ๋ฉฐ ๋ง์ฝ new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ์ง ์์ผ๋ฉด ์ผ๋ฐ ํจ์๋ก ๋์
function Student(name, age) {
this.name = name; // ์ฌ๊ธฐ์์ this๋ค์ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด
this.age = age;
this.getInfo = function() {
return `${this.name}๋ ${this.age}์ธ ์
๋๋ค.`;
}
}
const student = Student('์ ์งฑ๊ตฌ', 5); // ์ผ๋ฐ ํจ์
// ์ผ๋ฐ ํจ์๋ก ํธ์ถ๋ Student๋ ๋ฐํ๋ฌธ์ด ์์ผ๋ฏ๋ก undefined๋ฅผ ๋ฐํ
console.log(student); // undefined ์ถ๋ ฅ (๋ฆฌํด๊ตฌ๋ฌธ์ด ์๊ธฐ ๋๋ฌธ)
// ์ผ๋ฐ ํจ์๋ก ํธ์ถ๋ Student ๋ด์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด
console.log(age); // 5
console.log(name); // ์ ์งฑ๊ตฌ
console.log(getInfo()); // ์ ์งฑ๊ตฌ๋ 5์ธ ์
๋๋ค.
๐โโ๏ธ ์์ฑ์ ํจ์๊ฐ new ์ฐ์ฐ์ ์์ด ํธ์ถ๋๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ฌ์ฉ
- new ์ฐ์ฐ์์ ํจ๊ป ์์ฑ์ ํจ์๋ก์จ ํธ์ถ๋๋ฉด ํจ์ ๋ด๋ถ์ new.target์ ํจ์ ์์
- new ์ฐ์ฐ์ ์์ด ์ผ๋ฐ ํจ์๋ก ํธ์ถ๋ ํจ์ ๋ด๋ถ์ new.target์ undefined
function Dog(name, age) {
if(!new.target) {
// new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถ๋์ง ์์ ๊ฒฝ์ฐ undefined์ด๋ฏ๋ก
// new ์ฐ์ฐ์์ ํจ๊ป ์์ฑ์ ํจ์๋ฅผ ์ฌ๊ท ํธ์ถํ์ฌ ์์ฑ ๋ ์ธ์คํด์ค๋ฅผ ๋ฐํ
return new Dog(name, age);
}
this.name = name;
this.age = age;
}
// new ์ฐ์ฐ์ ์์ด ํธ์ถํด๋ new.target์ ํตํด ์์ฑ์ ํจ์๋ก์จ ํธ์ถ๋จ
const dog = Dog('์น์ฆ', 2);
console.log(dog);
๐ Ref.
* ๋๋ถ๋ถ์ ๋นํธ์ธ ์์ฑ์ ํจ์(Object, String, Number, Boolean, Date,, ...)๋ new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถ๋์๋์ง ํ์ธ ํ ์ ์ ํ ๊ฐ์ ๋ฐํ
* String, Number, Boolean์ ๊ฒฝ์ฐ new ์ฐ์ฐ์ ์์ด ํธ์ถํ๋ฉด ๊ฐ์ฒด ๊ฐ์ด ์๋ ๋ฌธ์์ด, ์ซ์, ๋ถ๋ฆฌ์ธ ๊ฐ(์์ ๊ฐ)์ ๋ฐํ. '์ด๋ฅผ ๋ฐ์ดํฐ ํ์
๋ณํ์ ํ์ฉ'