๐Ÿ“š13์ฃผ์ฐจ ์Šคํ„ฐ๋”” ์ค€๋น„[17์žฅ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ]

pjw__98ยท2024๋…„ 2์›” 29์ผ
1
post-thumbnail

๐Ÿ“š์Šคํ„ฐ๋”” ๋ช…

๋ชจ๋˜ํ•  ์ˆ˜ ์žˆ๋Š”๊ฑฐ ๋งž๋Š”๊ฑฐ๋‹ˆ?

๐Ÿ“† ํ•™์Šต ๋‚ ์งœ

2024/02/229(๋ชฉ) 5:00 PM

๐Ÿ“– 13์ฃผ์ฐจ ํ•™์Šต ๋ฒ”์œ„

17์žฅ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ(234.page ~ 246.page)


๐ŸŽฏ Object ์ƒ์„ฑ์ž ํ•จ์ˆ˜

์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ž€ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ Object ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

<script>
const usersData = new Object();
</script>

์œ„์™€ ๊ฐ™์ด ๋นˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ (.)ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•ด์„œ ํ”„๋กœํผํ‹ฐ ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์™„์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

<script>
const usersData = new Object();

userData.name = 'park';
usersData.sayHello = function() {
console.log('Hi! my name is' + this.name); 
};

console.log(userData); // {name : 'park', sayHello: f}
userData.sayHello(); // Hi! my name is park
</script>

์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•ด ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ธ์Šคํ„ด์Šค๋ผ ํ•œ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” Object ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์™ธ์—๋„ String, Number, Boolean, Function, Array, Date, RegExp, Promise ๋“ฑ์˜ ๋นŒํŠธ์ธ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

๐Ÿ“Œ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ๋ฌธ์ œ์ 

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์‹์€ ๊ฐ„ํŽธํ•˜์ง€๋งŒ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋งŒ ์ƒ์„ฑ๋œ๋‹ค.
๋”ฐ๋ผ์„œ ๋™์ผํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š” ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ๋งค๋ฒˆ ๊ฐ™์€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ธฐ์ˆ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„ํšจ์œจ์ ์ด๋‹ค.

<script>
const circle1 = { radius: 5, getDiameter() {
return 2 * this.radius; }
};
console.log(circle1.getDiameter()); // 10
const circle2 = { radius: 10, getDiameter() {
return 2 * this.radius; }
};
console.log(circle2.getDiameter()); // 20
</script>

๐Ÿ“Œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์‹์˜ ์žฅ์ 

์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์‹์€ ๋งˆ์น˜ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ
ํ…œํ”Œ๋ฆฟ(ํด๋ž˜์Šค)์ฒ˜๋Ÿผ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœํผํ‹ฐ ๊ตฌ์กฐ๊ฐ€ ๋™์ผํ•œ ๊ฐ์ฒด ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

<script>
function Circle(radius) {
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius; 
  };
}

// ์ธ์Šคํ„ด์Šค์˜ ์ƒ์„ฑ
const circle1 = new Circle(5); // ๋ฐ˜์ง€๋ฆ„์ด 5์ธ Circle ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
const circle2 = new Circle(10); // ๋ฐ˜์ง€๋ฆ„์ด 10์ธ Circle ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ 

console.log(circle1.getDiameter()); // 10 
console.log(circle2.getDiameter()); // 20
</script>

๐Ÿ“Œ this

this๋Š” ๊ฐ์ฒด ์ž์‹ ์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•œ ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜๋‹ค. this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’, ์ฆ‰ this ๋ฐ”์ธ๋”ฉ์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.

<script>
// ํ•จ์ˆ˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๋‹ค.
function foo() {
  console.log(this);
}
// ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ
// ์ „์—ญ ๊ฐ์ฒด๋Š” ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ๋Š” window, Node.js ํ™˜๊ฒฝ์—์„œ๋Š” global์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
foo(); // window
const obj = { foo }; // ES6 ํ”„๋กœํผํ‹ฐ ์ถ•์•ฝ ํ‘œํ˜„
// ๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœ
obj.foo(); // obj
// ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ
const inst = new foo(); // inst
</script>

๐Ÿ“Œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๊ณผ์ •

์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์—ญํ• ์€ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”(์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€ ๋ฐ ์ดˆ๊ธฐ๊ฐ’ ํ• ๋‹น)ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž.

<script>
// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
function Circle(radius) {
  // ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™”
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius; 
  };
}

// ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
const circle1 = new Circle(5); // ๋ฐ˜์ง€๋ฆ„์ด 5์ธ Circle ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
</script>

์ด ์ฝ”๋“œ์—์„œ๋Š” ๋„์ €ํžˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์€ ์•”๋ฌต์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค.

1.์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ this ๋ฐ”์ธ๋”ฉ

์•”๋ฌต์ ์œผ๋กœ ๋นˆ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์•”๋ฌต์ ์œผ๋กœ ์ƒ์„ฑ๋œ ๋นˆ ๊ฐ์ฒด, ์ฆ‰ ์ธ์Šคํ„ด์Šค๋Š” this์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

์ด ์ฒ˜๋ฆฌ๋Š” ํ•จ์ˆ˜ ๋ชธ์ฒด์˜ ์ฝ”๋“œ๊ฐ€ ํ•œ ์ค„์”ฉ ์‹คํ–‰๋˜๋Š” ๋Ÿฐํƒ€์ž„ ์ด์ „์— ์‹คํ–‰๋œ๋‹ค.

2.์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™”

์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ๊ธฐ์ˆ ๋˜์–ด ์žˆ๋Š” ์ฝ”๋“œ๊ฐ€ ํ•œ ์ค„์”ฉ ์‹คํ–‰๋˜์–ด this์— ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

์ฆ‰, this์— ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ๋Š” ์ธ์Šคํ„ด์Šค์— ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ดˆ๊ธฐ๊ฐ’์„ ์ธ์Šคํ„ด์Šค ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜์—ฌ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜ ๊ณ ์ •๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.

3.์ธ์Šคํ„ด์Šค ๋ฐ˜ํ™˜
์ƒ์„ฑ์ž ๋‚ด๋ถ€์˜ ๋ชจ๋“  ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚˜๋ฉด ์™„์„ฑ๋œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ this๊ฐ€ ์•”๋ฌต์ ์œผ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.

<script>
function Circle(radius) {
  // 1. ์•”๋ฌต์ ์œผ๋กœ ๋นˆ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  this์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
  
  // 2. this์— ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius; 
  };
  
  // 3. ์™„์„ฑ๋œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ this๊ฐ€ ์•”๋ฌต์ ์œผ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.
}


// ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ. Circle ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์•”๋ฌต์ ์œผ๋กœ this๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const circle1 = new Circle(5);
console.log(circle); // Circle {radius: 1, getDiameter: f}
</script>

๋งŒ์•ฝ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฉด this๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€ ๋ชปํ•˜๊ณ  ๋ช…์‹œํ•œ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

<script>
function Circle(radius) {
  // 1. ์•”๋ฌต์ ์œผ๋กœ ๋นˆ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  this์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
  
  // 2. this์— ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius; 
  };
  
  // 3. ๋ช…์‹œ์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์•”๋ฌต์ ์ธ this ๋ฐ˜ํ™˜์ด ๋ฌด์‹œ๋œ๋‹ค.
  return {}
}

// ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ. Circle ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const circle1 = new Circle(5);
console.log(circle); // {}
</script>

ํ•˜์ง€๋งŒ ๋ช…์‹œ์ ์œผ๋กœ ์›์‹œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์›์‹œ ๊ฐ’ ๋ฐ˜ํ™˜์€ ๋ฌด์‹œ๋˜๊ณ  ์•”๋ฌต์ ์œผ๋กœ this๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

<script>
function Circle(radius) {
  // 1. ์•”๋ฌต์ ์œผ๋กœ ๋นˆ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  this์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
  
  // 2. this์— ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius; 
  };
  
  // 3. ๋ช…์‹œ์ ์œผ๋กœ ์›์‹œ ๊ฐ’์„ ๋ฐ˜ํ™˜๋˜๋ฉด ์•”๋ฌต์ ์œผ๋กœ this๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
  return 100;
}

// ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ. Circle ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์•”๋ฌต์ ์œผ๋กœ this๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const circle1 = new Circle(5);
console.log(circle); // Circle {radius: 1, getDiameter: f}
</script>

๋”ฐ๋ผ์„œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์—์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
์ด๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ๋™์ž‘์„ ํ›ผ์†ํ•œ๋‹ค.

๐Ÿ“Œ ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ [[Call]]๊ณผ [[Construct]]

ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด์ด๋ฏ€๋กœ ์ผ๋ฐ˜ ๊ฐ์ฒด์™€ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

<script>
// ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด๋‹ค.
function foo() {}

// ๋”ฐ๋ผ์„œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์†Œ์œ ํ•  ์ˆ˜ ์žˆ๊ณ ,
foo.prop = 10;

// ๋ฉ”์„œ๋“œ๋„ ์†Œ์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.
foo.method = function() {
	console.log(this.prop);
}

foo.method(); // 10
</script>

๊ทธ๋Ÿฌ๋‚˜ ์ผ๋ฐ˜ ๊ฐ์ฒด๋Š” ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์ง€๋งŒ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ํ•จ์ˆ˜ ๊ฐ์ฒด๋งŒ์„ ์œ„ํ•œ ๋‚ด๋ถ€ ์Šฌ๋กฏ ([[Environment]], [[FormalParameters]]) ๋“ฑ์˜ ๋‚ด๋ถ€ ์Šฌ๋กฏ๊ณผ ([[Call]], [[Construct]])๊ฐ™์€ ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

ํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๋˜๋ฉด ํ•จ์ˆ˜ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ [[Call]]์ด ํ˜ธ์ถœ๋˜๊ณ 

new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๋˜๋ฉด ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ [[Construct]]๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

<script>
function foo() {}

// ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ: [[Call]]์ด ํ˜ธ์ถœ๋œ๋‹ค.
foo();

// ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ: [[Construct]]๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
new foo();
</script>

๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ [[Call]]์„ ๊ฐ–๋Š” ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ callable์ด๋ผ ํ•˜๋ฉฐ,

๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ [[Construct]]๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ constructor, ์•„๋‹Œ ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ non-constructor๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์ฆ‰, ๋ชจ๋“  ํ•จ์ˆ˜ ๊ฐ์ฒด๋Š” ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ชจ๋“  ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

๐Ÿ“Œconstructor์™€ non-constructor์˜ ๊ตฌ๋ถ„

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์€ ํ•จ์ˆ˜ ์ •์˜๋ฅผ ํ‰๊ฐ€ํ•˜์—ฌ ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•จ์ˆ˜ ์ •์˜ ๋ฐฉ์‹์— ๋”ฐ๋ผ ํ•จ์ˆ˜๋ฅผ constructor์™€ non-constructor๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.

constructor: ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ, ํ•จ์ˆ˜ ํ‘œํ˜„์‹, ํด๋ž˜์Šค(ํด๋ž˜์Šค๋„ ํ•จ์ˆ˜๋‹ค)
non-constructor: ๋ฉ”์„œ๋“œ(ES6 ๋ฉ”์„œ๋“œ ์ถ•์•ฝ ํ‘œํ˜„), ํ™”์‚ดํ‘œ ํ•จ์ˆ˜

<script>
// ์ผ๋ฐ˜ ํ•จ์ˆ˜ ์ •์˜: ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ, ํ•จ์ˆ˜ ํ‘œํ˜„์‹ 
function foo() {} 
const bar = function () {}; 
// ํ”„๋กœํผํ‹ฐ x์˜ ๊ฐ’์œผ๋กœ ํ• ๋‹น๋œ ๊ฒƒ์€ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ์ •์˜๋œ ํ•จ์ˆ˜๋‹ค. ์ด๋Š” ๋ฉ”์„œ๋“œ๋กœ ์ธ์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค. 
const baz = { 
  x: function () {} 
}; 

// ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ์ •์˜๋œ ํ•จ์ˆ˜๋งŒ์ด constructor์ด๋‹ค. 
new foo(); // -> foo {} 
new bar(); // -> bar {} 
new baz.x(); // -> x {} 

// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์ •์˜ 
const arrow = () => {}; 
new arrow(); // TypeError: arrow is not a constructor 

// ๋ฉ”์„œ๋“œ ์ •์˜: ES6์˜ ๋ฉ”์„œ๋“œ ์ถ•์•ฝ ํ‘œํ˜„๋งŒ์„ ๋ฉ”์„œ๋“œ๋กœ ์ธ์ •ํ•œ๋‹ค. 
const obj = { 
  x() {} 
}; 

obj.x(); // TypeError: obj.x is not a constructor
</script>

๐Ÿ“Œnew ์—ฐ์‚ฐ์ž

์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ํŠน๋ณ„ํ•œ ํ˜•์‹์  ์ฐจ์ด๋Š” ์—†๋‹ค.

๋‹จ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋Š” non-constructor๊ฐ€ ์•„๋‹Œ constructor์ด์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์“ธ๋•Œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ฒซ ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๊ธฐ์ˆ ํ•˜๋Š” ํŒŒ์Šค์นผ ์ผ€์ด์Šค๋กœ ๋ช…๋ช…ํ•˜๋Š” ๊ฒƒ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ถŒ์žฅ ํ•œ๋‹ค.

๐Ÿ“Œnew.target

ํ•˜์ง€๋งŒ ํŒŒ์Šค์นผ ์ผ€์ด์Šค ์ปจ๋ฒค์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค ํ•˜๋”๋ผ๋„ ์‹ค์ˆ˜๋Š” ์–ธ์ œ๋‚˜ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ์œ„ํ—˜์„ฑ์„ ํšŒํ”ผํ•˜๊ธฐ ์œ„ํ•ด ES6์—์„œ๋Š” new.target์„ ์ง€์›ํ•œ๋‹ค.

new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๋˜๋ฉด ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ new.target์€ ํ•จ์ˆ˜ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. new ์—ฐ์‚ฐ์ž ์—†์ด ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ new.target์€ undefined๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด์“ฐ๋ฉด ์œ„์™€ ๊ฐ™์€ ์œ„ํ—˜์„ฑ์„ ํšŒํ”ผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

<script>
// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
function Circle(radius) {
  // ์ด ํ•จ์ˆ˜๊ฐ€ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด new.target์€ undefined๋‹ค.
  if(!new.target) {
    // new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    return new Circle(radius);
  }
  
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius; 
  };
}

// new ์—ฐ์‚ฐ์ž ์—†์ด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ๋„ new.target์„ ํ†ตํ•ด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๋œ๋‹ค.
const circle = Circle(5);
console.log(circle.getDiameter());
</script>

๐Ÿ“Œ์Šค์ฝ”ํ”„ ์„ธ์ดํ”„ ์ƒ์„ฑ์ž ํŒจํ„ด

IE์—์„œ๋Š” new.target์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฐ ์ƒํ™ฉ์ด๋ผ๋ฉด ์Šค์ฝ”ํ”„ ์„ธ์ดํ”„ ์ƒ์„ฑ์ž ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

<script>
// Scope-Safe Constructor Pattern
function Circle(radius) {
  // ์ด ํ•จ์ˆ˜๊ฐ€ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด this๋Š” ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  if(!(this instance of Circle)) {
    // new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    return new Circle(radius);
  }
  
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius; 
  };
}

// new ์—ฐ์‚ฐ์ž ์—†์ด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ๋„ new.target์„ ํ†ตํ•ด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ๋œ๋‹ค.
const circle = Circle(5);
console.log(circle.getDiameter());
</script>

๋Œ€๋ถ€๋ถ„์˜ ๋นŒํŠธ์ธ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœ๋˜์—ˆ๋Š”์ง€๋ฅผ ํ™•์ธ ํ›„ ์ ์ ˆํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, Object์™€ Function ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” new ์—ฐ์‚ฐ์ž ์—†์ด ํ˜ธ์ถœํ•ด๋„ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ์™€ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ String, Number, Boolean ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ–ˆ์„ ๋•Œ String, Number, Boolean ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ new์—ฐ์‚ฐ์ž ์—†์ด ํ˜ธ์ถœํ•˜๋ฉด ๋ฌธ์ž์—ด, ์ˆซ์ž, ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

<script>
const str = String(123);
console.log(str, typeof str); // 123 string

const num = Number('123');
console.log(num, typeof num); // 123 number

const bool = Boolean('true');
console.log(bool, typeof bool); // true boolean
</script>

๐Ÿฅ„ ์†Œ๊ฐ ํ•œ์Šคํ‘ผ..

์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฑ… ๊ธฐ๋ฐ˜์œผ๋กœ ํ•™์Šต ํ•˜๋‹ค๋ณด๋‹ˆ ์‰ฌ์šฐ๋ฉด์„œ๋„ ์–ด๋ ค์›€.. ๋‡Œ ๊ณผ๋ถ€ํ•˜ ๊ฑธ๋ฆผ๐Ÿ’ซ

๐Ÿ‘๐ŸปReference
๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ - ์ด์›…๋ชจ ์ง€์Œ

profile
โ˜„๏ธ

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