์๋ฐ์คํฌ๋ฆฝํธ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ด 3๊ฐ์ง๋ ์์ต๋๋ค.
3๊ฐ์ง ๋ฐฉ๋ฒ ๋ชจ๋ ์ฉ๋๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ํ๋ฒ ๋ค๋ค๋ณด๊ฒ ์ต๋๋ค.
const ๊ฐ์ฒด๋ช
= {
ํ๋กํผํฐ๋ช
1: ๊ฐ1,
ํ๋กํผํฐ๋ช
2: ๊ฐ2,
...
ํ๋กํผํฐ๋ช
n: function(){
ํ๋กํผํฐ ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉด ์คํํ ๋ฌธ์ฅ;
...
}
}
๐ฅ ํ๋กํผํฐ(property)
- ์ด๋ฆ๊ณผ ๊ฐ์ผ๋ก ๊ตฌ์ฑ๋ ์ ๋ ฌ๋์ง ์์ ์งํฉ
- ํ๋กํผํฐ๋ ํจ์๋ ์ ์ฅํ ์ ์์(ํ๋กํผํฐ ๋ฉ์๋)
(ex) const userid = {'no':1, 'name':'๊น์ฌ๊ณผ', 'age':20, 'gender':'์ฌ์'}
function ์์ฑ์๋ช
(๋งค๊ฐ๋ณ์1, ๋งค๊ฐ๋ณ์2, ..){
this.ํ๋กํผํฐ๋ช
1 = ๊ฐ1;
this.ํ๋กํผํฐ๋ช
2 = ๊ฐ2;
...
this.ํ๋กํผํฐ๋ช
n: function(){
ํ๋กํผํฐ ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉด ์คํํ ๋ฌธ์ฅ;
...
}
}
const ํด๋์ค๋ช
= class {
constructor(๋งค๊ฐ๋ณ์1, ๋งค๊ฐ๋ณ์2 ..){
this.ํ๋กํผํฐ๋ช
1 = ๊ฐ1;
this.ํ๋กํผํฐ๋ช
2 = ๊ฐ2;
...
}
๋ฉ์๋๋ช
(๋งค๊ฐ๋ณ์1, ๋งค๊ฐ๋ณ์2 ..){
๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉด ์คํํ ๋ฌธ์ฅ;
...
}
}
const ๊ฐ์ฒด๋ช
1 = new ํด๋์ค๋ช
(๊ฐ1, ๊ฐ2 ..);
const ๊ฐ์ฒด๋ช
2 = new ํด๋์ค๋ช
(๊ฐ1, ๊ฐ2 ..);
const dog ={
name: '๋ฃจ์',
age: 12,
color: 'white',
weight: 3.5,
birthday: '20091210',
getBirthday: function(){
return this.birthday;
}
}
์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ: Object๋ช .Property๋ช
console.log(dog.name);
console.log(dog.age);
console.log(dog.color);
console.log(dog.weight);
๋๋ฒ์งธ ๋ฐฉ๋ฒ: Object๋ช ['Property๋ช ']
console.log(dog['name']);
console.log(dog['age']);
console.log(dog['color']);
console.log(dog['weight']);
+ ๋์ ์ผ๋ก ์์ฑ์ ์ ๊ทผํ๊ณ ์ถ์ ๋
function getValue(obj, key){
// return obj.key .ํํ ๋ถ๊ฐ๋ฅ
return obj[key];
}
console.log(getValue(dog, 'name'));
for(let i in Object๋ช
){
console.log(`i:${i}, dog[i]: ${Object๋ช
[i]}`);
}
ํจ์๊ฐ ์ ์ฅ๋์์๋๋ ํจ์ ์์ฒด๊ฐ ์ ์ฅ๋๋ค
ex)i:getBirthday, dog[i]: function(){return this.birthday;}
ํ๋กํผํฐ ๋ฉ์๋ -- ๋ณ์์ฒ๋ผ ์ธ์์๋ค๋๊ฒ ํต์ฌํฌ์ธํธ!
console.log(dog.getBirthday()); // 20091210
console.log(dog.getBirthday); // [Function: getBirthday]
function Dog(name, color){
this.name = name;
this.color = color;
this.play = function(){
return `${this.name}๋ ๋๋๋ค`;
}
}
// ์ฌ๋ฌ ๊ฐ์์ง๋ค ๋ง๋ค ์ ์์
const Rucy = new Dog('๋ฃจ์', 'white');
console.log(Rucy.name);
console.log(Rucy.color);
console.log(Rucy.play());
const PPomi = new Dog('๋ฝ๋ฏธ', 'black');
console.log(PPomi.name);
console.log(PPomi.color);
console.log(PPomi.play());
const Dog2 = class{
constructor(name, age, color){
this.name = name;
this.age = age;
this.color = color;
}
play(){
return `${this.name}์ ์ฆ๊ฒ๊ฒ ๋๋๋ค`;
}
}
const PPory = new Dog2('๋ฝ๋ฆฌ', 4, 'white');
console.log(PPory.name);
console.log(PPory.age);
console.log(PPory.color);
console.log(PPory.play());
// dog์ ๊ฐ์ฒด์ด๊ณ , ์ด๋ค ํด๋์ค์ ์ธ์คํด์ค๋ ์๋
let dog = {
name: '๋ฃจ์',
age: 13,
'dog-weight': 3.5,
['dog-height']: 0.8 //๋๊ดํธ๋ ๊ฐ๋ฅ
}
//ํ๋์ถ๊ฐ
dog.family = 'ํฌ๋ฉ';
console.log(dog.family); //ํฌ๋ฉ .์ผ๋ก์ ๊ทผ
console.log(dog['family']); //ํฌ๋ฉ []๋ก์ ๊ทผ
//ํ๋์ญ์
delete dog['dog-height'];
console.log(dog['dog-height']); //undefined ์ญ์ ์์ผ์ ์ฌ๋ผ์ง
class Fruit {
//ํ๋
eng;
//static ํ๋ฌํผํฐ
static count_fruits = 0;
//์์ฑ์
constructor(name, emoji){
this.name = name;
this.emoji = emoji;
}
//๋ฉ์๋
display = () => {
console.log(`${this.name}: ${this.emoji}`);
};
//static ๋ฉ์๋
static makeBanana(){
//ํด๋์ค ๋ ๋ฒจ ๋ฉ์๋( new )์์๋ this๋ฅผ ์ฐธ์กฐํ ์ ์์
return new Fruit('banana', '๐');
}
}
// apple์ Fruit ํด๋์ค์ ์ธ์คํด์ค
const apple = new Fruit('apple', '๐');
// orange๋ Fruit ํด๋์ค์ ์ธ์คํด์ค
const orange = new Fruit('orange', '๐');
const banana = Fruit.makeBanana();
console.log(banana);
// Fruit { display: [Function: display], name: 'banana', emoji: '๐' }
class Dog{
#name;
#color;
constructor(name, color){
this.#name = name;
this.#color = color;
}
run = () => {
console.log(`${this.#color} ์์์ ๊ฐ์์ง ${this.#name}๋ ๋ฌ๋ฆฝ๋๋ค`);
}
#eat = () =>{
console.log(`${this.name}๋ ๋จน์ต๋๋ค.`)
}
get info() {
return `์ด๋ฆ:${this.#name}, ์์:${this.#color}`;
}
set info(value) {
console.log('set', value);
this.#name = value;
}
}
const Rucy = new Dog('๋ฃจ์', 'white');
console.log(Rucy); //Dog { run: [Function: run] } : # ๋ชป๋ณธ๋ค
Rucy.name = '๊ฐ๋ฅ์ด'; //public์ผ๋ก ์ด๋ฆ์ ์์ฑ
// Rucy.#name = '๊ฐ๋ฅ์ด'; //#field๋ ์ธ๋ถ์์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํจ
//SyntaxError: Private field '#name' must be declared in an enclosing class ์๋ฌ๋ธ
console.log(Rucy); //Dog { run: [Function: run], name: '๊ฐ๋ฅ์ด' }
Rucy.run();
// Rucy.eat(); //#function()์ ์ธ๋ถ์์ ์ ๊ทผ ๋ถ๊ฐ๋ฅ!
//TypeError: Rucy.eat is not a function
// console.log(Rucy.info()); //XX
console.log(Rucy.info); //getterํจ์ ํธ์ถ๋ฐฉ๋ฒ
Rucy.info= '๋ฝ๋ฏธ'; //set์ญํ ํจ์
console.log(Rucy.info); ;