๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ—‚.๋Œ€์™ธํ™œ๋™/๐Ÿ“.์นด์นด์˜คํด๋ผ์šฐ๋“œ์Šค์ฟจ

[JavaScript] ๊ฐ์ฒด

by ๐Ÿ’พ๊ณ ๊ตฌ๋งˆ๋ง›ํƒ•๋จน๊ณ ์‹ถ๋‹ค 2024. 1. 17.
728x90

๐Ÿ“– ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด ์ •์˜

์ž๋ฐ”๋Š” ์ƒ์†์„ ์ œ์–ด์™€ ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์˜ค๋กœ์ง€ ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ(๊ณต์œ )์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

 

๐Ÿ“•๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹: ES3

var pobj ={
	x: 100,
	y: 200,
	myShape: 'Player',
	display: function(){
		document.write("<h1>"+this.x+", "+this.y+"="+this.myShape+"</h1>")
	}
};
pobj.display();

 

๐Ÿ“— ์ƒ์„ฑ์žํ•จ์ˆ˜ ํŒจํ„ด์˜ ๊ฐ์ฒด ์ƒ์„ฑ: ES5

// java: ClassType object = new ClassType();
function Person(){ // ์ƒ์„ฑ์žํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ์ƒ๊ฐ.
	// ์ธ์Šคํ„ด์Šค์˜ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์ดˆ๊ธฐํ™”
	this.x= 100; // ํ•จ์ˆ˜์˜ ์ƒ์„ฑ์žํ•จ์ˆ˜ ํŒจํ„ด์ธ ๊ฒฝ์šฐ this๋ฅผ ๊ผญ ์จ์„œ ์„ ์–ธํ•ด์•ผํ•œ๋‹ค.(= ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ์œ„ํ•ด์„œ๋Š” this ํ•„์š”)
	this.y= 200;
	this.myShape= 'Player';
	this.display = function(){
		let color = 'green'; // ์ง€์—ญ๋ณ€์ˆ˜
		document.write('x=' + this.x);
		document.write(', y=' + this.y);
		document.write(', myShape' + this.myShape);
		document.write(', color=' + color); // ์ง€์—ญ๋ณ€์ˆ˜๋‹ˆ๊นŒ this ์ œ์™ธ
		document.write('<hr>');
	};
};

let pObj = new Person(); // ES5์˜ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์‹
pObj.display();

 

ํ•˜์ง€๋งŒ ํ•ด๋‹น ๋ฐฉ๋ฒ•์ด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด ๋น„ํšจ์œจ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— prototype์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

ProtoType์€ ์ฝ”๋“œ๋ฅผ ๋„ฃ๊ณ  ๊ณต์œ ํ•˜๋Š” ๊ณต๊ฐ„์ด๋‹ค.

 

๋”๋ณด๊ธฐ

1. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

Person.prototype.getName= function(){
	return this.name;
};

Person.prototype.setName= function(value){
	this.name=value;
};

ํ•ญ์ƒ prototype์„ ํƒ€์ดํ•‘ํ•˜๊ธฐ ๊ท€์ฐฎ๊ธฐ๋•Œ๋ฌธ์— Function๊ฐ์ฒด์— ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ๋‹ค.


2. prototype๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
๋ชจ๋“  ํ•จ์ˆ˜๊ฐ์ฒด?๋Š” Function.prototype์„ ๊ณต์œ ํ•œ๋‹ค. 
๋”ฐ๋ผ์„œ Function.prototype์— ์ž‘์„ฑํ•˜๋ฉด ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๊ณต๋™์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

 

๐Ÿ“— ProtoType ์‚ฌ์šฉ

ํ•ญ์ƒ .prototype์„ ์ž‘์„ฑํ•˜์ง€์•Š๊ณ  Function ๊ฐ์ฒด์— prototype์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์—ฐ๊ฒฐ๋˜๋„๋ก ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

// ES5: ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํŒจํ„ด
function Person(arg){
	this.name= arg;
};

// โญ
Function.prototype.method= function(name, func){
	this.prototype[name]= func;
}

Person.method("setName", function(value){
	this.name=value;
});

Person.method("getName", function(value){
	return this.name+": Person";
});

var man= new Person("๋‚จ์ž");
console.log(man.getName());

 

๐Ÿ“˜ Class ์‚ฌ์šฉ: ES6

extends๋Š” prototype ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘: Rabbit.prototype.[[Prototype]]์„ Animal.prototype์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. [[Prototype]]: ํฌ์ธํŒ… ํ•œ๋‹ค๋Š” ์˜๋ฏธ, ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค __proto__๋ฅผ ์ง€์›ํ•˜์ง€์•Š๊ธฐ๋„ ํ•ด์„œ ๊บฝ์‡ ๋กœ ํ‘œํ˜„ํ•จ.

class Animal{
    constructor(name){
    	this.speed= 0;
    	this.name= name;
    }
    run(speed){
        this.speed= speed;
        alert(`${this.name}์€/๋Š” ์†๋„ ${this.speed}๋กœ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.`);
    }
    stop(){
        this.spped= 0;
        alert(`${this.name}์ด/๊ฐ€ ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค.`);
    }
}
class Rabbit extends Animal{ 
    hide(){
        alert(`${this.name}์ด/๊ฐ€ ์ˆจ์—ˆ์Šต๋‹ˆ๋‹ค.`);
    }
}

let bear= new Animal("๊ณฐ");
let whiteRabbit= new Rabbit("ํฐ ํ† ๋ผ");

bear.run(10);
// bear.hide(); error

whiteRabbit.hide();
whiteRabbit.run(5);

 

[[์ฐธ๊ณ  ์ž๋ฃŒ]]

https://ko.javascript.info/class-inheritance

728x90