์ƒˆ์†Œ์‹

Languages/Java Script

[Js] ์ƒ์„ฑ์ž์™€ new | javascript์˜ ๊ฐ์ฒด ์ง€ํ–ฅ ํŠน์ง• | ๊ฐ์ฒด

  • -
๋ฐ˜์‘ํ˜•

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ "์ƒํ™œ์ฝ”๋”ฉ"์˜ ์ด๊ณ ์ž‰ ๋‹˜์˜ ๊ฐ•์˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์˜ฌ๋ฆฐ ๊ฒŒ์‹œ๊ธ€ ์ž…๋‹ˆ๋‹ค.

์ƒ์—…์ ์ธ ์šฉ๋„๊ฐ€ ์•„๋‹Œ, ๊ฐœ์ธ ๊ณต๋ถ€ ์ •๋ฆฌ ๋ชฉ์ ์œผ๋กœ ์˜ฌ๋ฆฌ๋Š” ๊ธ€์ž„์„ ๋ฏธ๋ฆฌ ์•Œ๋ฆฝ๋‹ˆ๋‹ค.

 

 

 

 


 

์ƒ์„ฑ์ž์™€ new

๊ฐ์ฒด

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์˜ ์†ํ•˜๋Š” ๊ณ„์—ด์˜ ์–ธ์–ด๋“ค์€ prototype - based programming์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

๊ฐ์ฒด๋ž€ ์„œ๋กœ ์—ฐ๊ด€๋œ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ๊ทธ๋ฃนํ•‘ํ•œ ๊ทธ๋ฆ‡์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด ๋‚ด์˜ ๋ณ€์ˆ˜๋ฅผ ํ”„๋กœํผํ‹ฐ(property) ํ•จ์ˆ˜๋ฅผ ๋ฉ”์†Œ๋“œ(method)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

 

<script>
      var person = {}
      person.name = 'egoing';
      person.introduce = function(){
          return 'My name is '+this.name;
      }
      document.write(person.introduce());
</script>

์œ„ ์ฝ”๋“œ์—์„œ this๋Š” ํ•จ์ˆ˜๊ฐ€ ์†ํ•ด์žˆ๋Š” ๊ฐ์ฒด ์ฆ‰ person์ด๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋‹ด๊ณ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์— ๋ถ„์‚ฐ๋˜์–ด ์žˆ๋‹ค. ๊ฐ์ฒด๋ฅผ ์ •์˜ ํ•  ๋•Œ ๊ฐ’์„ ์…‹ํŒ…ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด์ž.

<script>
    var person = {
        'name' : 'egoing',
        'introduce' : function(){
            return 'My name is '+this.name;
        }
    }
    document.write(person.introduce());
</script>

๊ฐ€๋…์„ฑ๋„ ์ข‹๊ณ , ๋‚ด์šฉ์ด ์ค‘๊ฐ„์— ๋ผ์–ด๋“ค๊ฑฐ๋‚˜ ๋ณ€์กฐ๋˜๋Š” ์ƒํ™ฉ์„ ์กฐ๊ธˆ ๋ฐฉ์ง€ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

 

์œ„์™€ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ํ•ด๋ณด์ž.

ํ•˜์ง€๋งŒ name์ด๋ผ๋Š” ๋ถ€๋ถ„๊ณผ ์•„๋ž˜ introduce๊ฐ€ ์ค‘๋ณต๋œ๋‹ค.

์—„์—ฐํžˆ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์ง€๋งŒ ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„์ด ๋งŽ์ด ์ƒ๊ธด๋‹ค๋Š” ๋œป์ด๋‹ค. 

 

์ฆ‰ ์ฝ”๋“œ์˜ ์–‘์ด ๋งŽ์•„์ง€๊ณ , ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ค์›Œ์ง€๋ฉฐ, ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ด๋ ‡๋“ฏ ๋งŒ์•ฝ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋‹ด์„ ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด ๊ฐ์ฒด์˜ ์ •์˜๋ฅผ ๋ฐ˜๋ณตํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

๊ฐ์ฒด์˜ ๊ตฌ์กฐ๋ฅผ ์žฌํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•˜๋‹ค. ์ด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ƒ์„ฑ์ž๋‹ค.

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ผ๋Š” ์–ธ์–ด๋ฅผ ํ•จ์ˆ˜ํ˜• ์–ธ์–ด๋ผ๊ณ  ํ•˜๊ณ , ํ•จ์ˆ˜๋ผ๋Š” ์–ธ์–ด๋ฅผ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ๊นŒ์ง€ ํ•œ๋‹ค๋Š” ๊ฒƒ์€

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ•จ์ˆ˜๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์œ„์ƒ์„ ์•”์‹œํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ดํ•ดํ•˜๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ‚ค์›Œ๋“œ๋Š” "๊ฐ์ฒด"์ด๊ธฐ ์ „์— "ํ•จ์ˆ˜"์ด๋‹ค.

 

 

 

์ƒ์„ฑ์ž

 

์ƒ์„ฑ์ž(constructor)๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋‹ค.

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

<!DOCTYPE html>
<html>
<head>
	<title>์ƒ์„ฑ์ž</title>
</head>
<body>
<script type="text/javascript">
	function Person(){}// Person์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ํ•จ์ˆ˜๋กœ ์ฐฝ์กฐ. 
	var p = new Person();//new๊ฐ€ ๋ถ™์œผ๋ฉด ๋’ค์— ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑ์ž๋ผ๊ณ  ๋ถ€๋ฆ„.== ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž  
	p.name = 'haeun';
	p.introduce = function(){
		return 'My name is '+this.name; //ํ•จ์ˆ˜์—์„œ this๋Š” ํ•จ์ˆ˜๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” ๊ฐ์ฒด ์ฆ‰ p์ด๋‹ค.
	}
	document.write(p.introduce());

</script>
</body>
</html>

 

๋งŒ์ผ ์œ„ ์ฝ”๋“œ์—์„œ new๋ผ๋Š” ๊ฒƒ์„ ๋ถ™์ด์ง€ ์•Š๊ณ  ์ž…๋ ฅํ•ด๋ณธ๋‹ค๋ฉด, defined๋ผ๊ณ  ์ถœ๋ ฅ๋œ๋‹ค.

์ด๋Š”, ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

ํ•˜์ง€๋งŒ new๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๋ฉด ์ƒˆ๋กœ์šด ๊ฐ์ฒด(๋น„์–ด์žˆ๋Š” ๊ฐ์ฒด)๋ฅผ ๋งŒ๋“  ํ›„์— ์ด๋ฅผ ๋ฆฌํ„ด(๋ฐ˜ํ™˜)ํ•ด์ค€๋‹ค.

 

์œ„์˜ ์ฝ”๋“œ๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋ณ€์ˆ˜ p์— ๋‹ด์•˜๋‹ค.(Person()์ด๋ผ๋Š” ๊ฐ์ฒด๋Š” p์— ๋‹ด์€ ๊ฒƒ)

 

์—ฌ๋Ÿฌ์‚ฌ๋žŒ์„ ์œ„ํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

<!DOCTYPE html>
<html>
<head>
	<title>์ƒ์„ฑ์ž</title>
</head>
<body>
<script type="text/javascript">
	function Person(){}
	var p1 = new Person();//1๋ฒˆ์งธ ๊ฐ์ฒด ์ƒ์„ฑ 
	p1.name = 'haeun';
	p1.introduce = function(){
		return 'My name is '+this.name;
	}
	document.write(p1.introduce()+"<br />");

	var p2 = new Person(); //2๋ฒˆ์งธ ๊ฐ์ฒด ์ƒ์„ฑ
	p2.name = 'admin';
	p2.introduce = function(){
		return 'My name is '+this.name;
	}
	document.write(p2.introduce());

</script>
</body>
</html>

๋ณ„๋กœ ๊ฐœ์„ ๋œ ๊ฒƒ์ด ์—†๋‹ค.

 

 

์ƒ์„ฑ์ž ๋‚ด์—์„œ ์ด ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์ดˆ๊ธฐํ™”๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด์„œ ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋Œ€ํญ ๋†’์•„์กŒ๋‹ค.

<!DOCTYPE html>
<html>
<head>
	<title>์ƒ์„ฑ์ž</title>
</head>
<body>
<script type="text/javascript">
	function Person(name){
		this.name = name;
		this.introduce = function(){
			return 'My name is '+this.name+'<br />';
		}
	}
	var p1 = new Person('haeun'); //1๋ฒˆ์งธ ๊ฐ์ฒด 
	var p2 = new Person('admin'); //2๋ฒˆ์งธ ๊ฐ์ฒด
	document.write(p1.introduce());
	document.write(p2.introduce());
</script>
</body>
</html>

์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜์™€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฒซ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ์‹œํ•œ๋‹ค.

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, introduce๋ผ๋Š” ๋ฉ”์†Œ๋“œ ์ฝ”๋“œ๊ฐ€ ๋‹จ ํ•œ๋ฒˆ๋งŒ ์ •์˜ํ•˜์˜€๋‹ค. -> ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋ฉฐ ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋˜์–ด ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค.

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ์ž์˜ ํŠน์ง•

์ผ๋ฐ˜์ ์ธ ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด์—์„œ ์ƒ์„ฑ์ž๋Š” ํด๋ž˜์Šค์˜ ์†Œ์†์ด๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์ฃผ์ฒด๋Š” ํ•จ์ˆ˜๋‹ค.

ํ•จ์ˆ˜์— new๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์„ ํ†ตํ•ด์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ•จ์ˆ˜์˜ ์œ„์ƒ์„ ์•”์‹œํ•˜๋Š” ๋‹จ์„œ์ด๋ฉด์„œ ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ถ”๊ตฌํ•˜๋Š” ์ž์œ ๋กœ์›€์„ ๋ณด์—ฌ์ฃผ๋Š” ์‚ฌ๋ก€๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.