Bibi's DevLog ๐Ÿค“๐ŸŽ

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - ๊ฐ์ฒด 2 : ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž(์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ) ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Javascript ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - ๊ฐ์ฒด 2 : ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž(์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ)

๋น„๋น„ bibi 2020. 10. 20. 16:01
* ์ด ๊ธ€์€ ์ธํ”„๋Ÿฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜ธ๋ˆ…์Šค๋‹˜์˜ ์œ ๋ฃŒ ๊ฐ•์˜ '์‰ฝ๊ณ  ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฐฐ์›Œ๋ณด๋Š” Javascript ์ž…๋ฌธ - ์ฝ”๋“œ์Šค์ฟผ๋“œ ๋งˆ์Šคํ„ฐ์ฆˆ ์ฝ”์Šค ๋ ˆ๋ฒจ1'๋ฅผ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๊ฐ•์˜ ๋‚ด์šฉ์— ๋”ํ•ด, ์ œ๊ฐ€ ํ•„๊ธฐํ•˜๊ณ  ๊ตฌ๊ธ€๋งํ•œ ๋‚ด์šฉ์ด ์ •๋ฆฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๋ณด์ธ ์ œ๊ฐ€ ์ดํ•ดํ•œ ๋Œ€๋กœ ์ •๋ฆฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ถ€์ •ํ™•ํ•œ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Œ์„ ๊ฐ์•ˆํ•ด ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ธํ”„๋Ÿฐ ์ธก ๋‹ต๋ณ€์„ ๋ฐ›์€ ์ ์ด ์žˆ์œผ๋‚˜(https://bibi6666667.tistory.com/37), ์ €์ž‘๊ถŒ์ƒ ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค๋ฉด ๋ณดํ˜ธ ๋ชจ๋“œ(๋น„๊ณต๊ฐœ)๋กœ ์ „ํ™˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž ์‚ดํŽด๋ณด๊ธฐ


๋ณต์Šต

๊ฐ์ฒด object ๋Š” ์œ ์ง€ ๋ณด์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ’๋“ค์„ ๋ฌถ์–ด ๋†“์€ ๊ฒƒ์œผ๋กœ, ์†์„ฑproperty๊ณผ ๋ฉ”์†Œ๋“œmethod๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

var p1 = {
  "name": "honux", // ์†์„ฑ(ํ”„๋กœํผํ‹ฐ)
  "eat": function(food) { //  ๋ฉ”์†Œ๋“œ
  }; 
};

 

์ƒ์„ฑ์ž๋กœ ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ

var a = {"name" = "์•„์„œ์Šค", "hp" : 999999};

๊ฐ์ฒด๋ฅผ ํ•œ ๋‘๊ฐœ ๋งŒ๋“ค ๋•Œ๋Š” ๊ทธ๋ƒฅ ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ JSONํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ๋‚˜์—ดํ•ด ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ๋น„์Šทํ•œ ํ˜•์‹์˜ ๊ฐ์ฒด๋ฅผ '์—ฌ๋Ÿฌ ๊ฐœ' ๋งŒ๋“ค์–ด์•ผ ํ•  ๋•Œ, ๋งค๋ฒˆ ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๋ฒˆ๊ฑฐ๋กญ๋‹ค.
๋”ฐ๋ผ์„œ ๋น„์Šทํ•œ ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค ๋•Œ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜, ์ƒ์„ฑ์žConstructor๋ฅผ ์ด์šฉํ•œ๋‹ค.
(์ƒ์„ฑ์žConstructor : ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜)

-์ƒ์„ฑ์ž๋Š” ๊ด€๋ก€์ƒ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค.      

-์ƒ์„ฑ์ž์˜ ํ˜•ํƒœ

var ์ƒ์„ฑ์ž ์ด๋ฆ„ = function(์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค) {
    this.์†์„ฑ1 = ;
    this.์†์„ฑ2 = ;
    ...
    this.๋ฉ”์„œ๋“œ1 = function(๋ฉ”์„œ๋“œ1์˜ ๋งค๊ฐœ๋ณ€์ˆ˜) { ..
    };
    this.๋ฉ”์„œ๋“œ2 = function(๋ฉ”์„œ๋“œ2์˜ ๋งค๊ฐœ๋ณ€์ˆ˜) { ..
    };
};
//Human ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ํ•จ์ˆ˜ = ์ƒ์„ฑ์ž 
var Human = function(name, hp, power) { 
    this.name = name; 
    this.hp = hp;
    this.power = power; 
    this.attack = function(target) {
		console.log(this.name + "์ด " + target.name + "์„ ๊ณต๊ฒฉํ–ˆ๋‹ค.");
        target.hp -= this.power; 
    };
    this.show = function() {
        console.log("%s %d" , this.name, this.hp);
    }; 
};

 

โž• "%s $d" ์˜ ์˜๋ฏธ : ์ถœ๋ ฅํ˜•์‹(type)์„ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ.
%s = (string์˜ ์•ฝ์ž) ๋’ค์˜ '๋ฌธ์ž์—ด'์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ฆ‰ ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” this.name์„ ์ถœ๋ ฅ 
%d = (decimals = ์†Œ์ˆ˜ ์˜ ์•ฝ์ž) ๋’ค์˜ '์ˆซ์ž'๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ฆ‰ ์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” this.hp๋ฅผ ์ถœ๋ ฅ 

 

 

์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” new ๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

var p1 = new Human("honux", 100, 10);
var p2 = new Human("crong", 200, 1); 
var p3 = new Human("pobi", 50, 30);


p1.attack(p2); // honux์ด crong์„ ๊ณต๊ฒฉํ–ˆ๋‹ค.
p2; // Human {name: "crong", hp: 190, pow: 1, attack: f, show: f}
// (crong์˜ ์ฒด๋ ฅ์ด 10 ๊นŽ์˜€๋‹ค)

p3.attack(p2); // pobi์ด crong์„ ๊ณต๊ฒฉํ–ˆ๋‹ค.
p2; // Human {name: "crong", hp: 160, pow: 1, attack: f, show: f}
// (crong์˜ ์ฒด๋ ฅ์ด 30 ๊นŽ์˜€๋‹ค)

p1.show(); // [honux] 100
p2.show(); // [crong] 159
p3.show(); // [pobi] 50

์œ„ ์ฝ”๋“œ์—์„œ p1/p2/p3์€ "๊ฐ์ฒด" ๋˜๋Š” "(ํด๋ž˜์Šค์˜) ์ธ์Šคํ„ด์Šคinstance"๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
๋˜, p1/p2/p3์€ ๋ณ€์ˆ˜ ์ค‘์—์„œ๋„ "์ฐธ์กฐ ๋ณ€์ˆ˜"์ž…๋‹ˆ๋‹ค.
์ฐธ์กฐ ๋ณ€์ˆ˜๋Š” ๊ฐ์ฒด์˜ ๋ณ€์ˆ˜์ด๋ฉฐ, ์ฐธ์กฐ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ์— ๋‹ค์‹œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

์ฝ์„ ๋•Œ : Human ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค p1/p2/p3์ด๋‹ค.

โ— ์ธ์Šคํ„ด์Šค instance : ์˜ˆ/๊ฒฝ์šฐ/์‚ฌ๋ก€(โ‰“example, case) ๋ผ๋Š” ๋œป์ด๋‹ค.
-์ธ์Šคํ„ด์Šค๋ผ๋Š” ์šฉ์–ด ์ž์ฒด๋Š” '๊ฐ์ฒดobject'์™€ ์œ ์‚ฌํ•˜๋‚˜,
๊ฐ์ฒด๋Š” ๋” ์ผ๋ฐ˜์ ์ธ ์˜๋ฏธ์ด๋ฉฐ / ์ธ์Šคํ„ด์Šค๋Š” ํ˜„์žฌ ์ƒ์„ฑ๋œ ๊ทธ ๊ฐ์ฒด๋ผ๋Š” ๋” ๊ตฌ์ฒด์ ์ธ ์˜๋ฏธ์ด๋‹ค.

 

 

์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

  • ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค ๋•Œ๋Š” ๊ฐ„๋‹จํžˆ json ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด์„œ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

 

์˜ˆ์ œ

  1. Food(name, energy) ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.
  2. eat(food) ๋ฉ”์†Œ๋“œ๋ฅผ Human() ์ƒ์„ฑ์ž ์•ˆ์— ์ถ”๊ฐ€ํ•ด ๋ด…์‹œ๋‹ค.
  3. ๋„๋„ˆ์ธ  ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ํœด๋จผ ๊ฐ์ฒด์—๊ฒŒ ๋จน์—ฌ ๋ด…์‹œ๋‹ค.
///// ์˜ˆ์ œ ํ’€์ด - Human.eat(food)๋Œ€์‹  Food.feed(human) ์œผ๋กœ ๋ณ€ํ˜•.
var Food = function(name, energy) {
	this.name = name;
    this.energy = energy;
    this.feed = function(human) {
    	human.hp += this.energy;
    };
};

var d = new Food('donut', 30);
// ์ƒˆ๋กœ ๋งŒ๋“ค ๋•Œ๋Š” ๊ผญ new๋ฅผ ์ ๊ธฐ!
d // Food {name: "donut", energy: 30, feed: f}

p2; // Human {name: "crong", hp: 159, pow: 1, attack: f, show: f}


d.feed(p2);
p2; // Human {name: "crong", hp: 189, pow: 1, attack: f, show: f}
// d (๋„๋„›)์„ ๋จน์—ฌ์„œ crong์˜ hp๊ฐ€ 30 ์ฆ๊ฐ€ํ–ˆ๋‹ค.
d.feed(p2);
p2; // Human {name: "crong", hp: 219, pow: 1, attack: f, show: f}
// d (๋„๋„›)์„ ํ•œ ๋ฒˆ ๋” ๋จน์—ฌ์„œ crong์˜ hp๊ฐ€ 30 ๋” ์ฆ๊ฐ€ํ–ˆ๋‹ค.

 

 

๊ฐ์ฒด ์กฐ๊ธˆ ๋” ๊ณต๋ถ€ํ•˜๊ธฐ

๋” ๊ณต๋ถ€ํ•˜๊ณ  ์‹ถ์œผ์‹  ๋ถ„๋“ค์€ ์ƒํ™œ ์ฝ”๋”ฉ์˜ ์•„๋ž˜ ํŽ˜์ด์ง€๋ฅผ ๊ณต๋ถ€ํ•ด ๋ณด์„ธ์š”. https://opentutorials.org/course/743/6553

์ƒํ™œ์ฝ”๋”ฉ - ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์˜ค๋ฆฌ์—”ํ…Œ์ด์…˜ (1. ๊ฐ์ฒด / 2. ์„ค๊ณ„์™€ ์ถ”์ƒํ™” / 3. ๋ถ€ํ’ˆํ™”)

 

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์ƒํ™œ์ฝ”๋”ฉ

๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Object-Oriented Programming)์€ ์ข€ ๋” ๋‚˜์€ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจ๋Ÿฌ๋‹ค์ž„์œผ๋กœ ๋กœ์ง์„ ์ƒํƒœ(state)์™€ ํ–‰์œ„(behave)๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฐ์ฒด๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ๏ฟฝ

opentutorials.org