๐Ÿš€ [JavaScript] ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•

์ „์ฃผ์€ยท2022๋…„ 11์›” 28์ผ
0
post-thumbnail

๐Ÿš€ Javascript object

๐Ÿ˜Š ๋“ค์–ด๊ฐ€๋ฉด์„œ...

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด 3๊ฐ€์ง€๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค.
3๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ชจ๋‘ ์šฉ๋„๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ ๋‹ค๋ค„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

โœ” 1๋ฒˆ์งธ ๋ฐฉ๋ฒ•: ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฒ•

๐ŸŒˆ ๋”ฑ 1๊ฐœ๋งŒ ๋งŒ๋“ค๊ณ  ์‹ถ์„๋•Œ!

  1. ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฒ•
  • ๋ฆฌํ„ฐ๋Ÿด๋กœ ํ‘œ๊ธฐํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋Š” ๋ณต์ œ๋œ ๊ฒƒ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šค๋ผ๊ณ  ๋ถ€๋ฅด์ง€ ์•Š๋Š”๋‹ค.
const ๊ฐ์ฒด๋ช… = {
	ํ”„๋กœํผํ‹ฐ๋ช…1: ๊ฐ’1,
	ํ”„๋กœํผํ‹ฐ๋ช…2: ๊ฐ’2,
	...
	ํ”„๋กœํผํ‹ฐ๋ช…n: function(){
	ํ”„๋กœํผํ‹ฐ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์‹คํ–‰ํ•  ๋ฌธ์žฅ;
	...
	}
}

๐Ÿฅš ํ”„๋กœํผํ‹ฐ(property)

  • ์ด๋ฆ„๊ณผ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ง‘ํ•ฉ
  • ํ”„๋กœํผํ‹ฐ๋Š” ํ•จ์ˆ˜๋„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ(ํ”„๋กœํผํ‹ฐ ๋ฉ”์†Œ๋“œ)
    (ex) const userid = {'no':1, 'name':'๊น€์‚ฌ๊ณผ', 'age':20, 'gender':'์—ฌ์ž'}

โœ” 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•: ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

๐ŸŒˆ ์—ฌ๋Ÿฌ๋ฒˆ ๋ณต์‚ฌํ•ด ์‚ฌ์šฉ (์ž๋ฐ” ๊ฐ์ฒด๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ ๋น„์Šท)

  1. ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
  • java์—์„œ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค๋ผ๊ณ ํ•จ
function ์ƒ์„ฑ์ž๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜1, ๋งค๊ฐœ๋ณ€์ˆ˜2, ..){
	this.ํ”„๋กœํผํ‹ฐ๋ช…1 = ๊ฐ’1;
	this.ํ”„๋กœํผํ‹ฐ๋ช…2 = ๊ฐ’2;
	...
	this.ํ”„๋กœํผํ‹ฐ๋ช…n: function(){
    ํ”„๋กœํผํ‹ฐ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์‹คํ–‰ํ•  ๋ฌธ์žฅ;
    ...
	}
}

โœ” 3๋ฒˆ์งธ ๋ฐฉ๋ฒ•: ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

๐ŸŒˆ ์—ฌ๋Ÿฌ๋ฒˆ ๋ณต์‚ฌ

  1. ํด๋ž˜์Šค๋ฅผ ์ด์šฉ
  • ECMA Script6์— ์ถ”๊ฐ€๋œ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•
  • ๋‚ด๋ถ€์ ์œผ๋กœ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™
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;
    }
}

๐Ÿ ๊ฐ’์„ ์–ป์–ด์˜ค๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•

์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: 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๋ฌธ์œผ๋กœ key์™€ value๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•: for(let i in Object๋ช…){}

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  ์‚ญ์ œ์‹œ์ผœ์„œ ์‚ฌ๋ผ์ง

๐Ÿน ํด๋ž˜์Šค ์‘์šฉ - static

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: '๐ŸŒ' }

๐Ÿป ํด๋ž˜์Šค ์‘์šฉ - private

  • java์—์„œ๋Š” ํ•„๋“œ์˜ ๊ฐ’์„ ๋ชป๋ฐ”๊พธ๊ฒŒ private๋ฅผ ์ผ์ง€๋งŒ, javascript๋Š” #์„ ์‚ฌ์šฉ
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);  ;

0๊ฐœ์˜ ๋Œ“๊ธ€