Hello Constructor๐Ÿฉท

์ฐจ๋…ธยท2023๋…„ 8์›” 2์ผ
0

JS

๋ชฉ๋ก ๋ณด๊ธฐ
8/96

A constructor is a special function that creates and initializes an object instance of a class.

ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์„ ์–ธํ•˜๋Š” ํŠน๋ณ„ํ•œ ํŽ‘์…˜์ด๋‹ค.

The purpose of a constructor is to create a new object and set values for any existing object properties.

์ปจ์ŠคํŠธ๋Ÿญํ„ฐ์˜ ๋ชฉ์ ์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , ๊ฐ์ฒด ์„ฑ์งˆ์˜ ๊ฐ’์„ ์„ค์ •ํ•œ๋Š” ๊ฒƒ์ด๋‹ค.

What happens When A Constructor Gets Called?

์ปจ์ŠคํŠธ๋Ÿญํ„ฐ๋Š” ํ˜ธ์ถœ๋œ๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋Š”๊ฐ€?

When a constructor gets invoked in JavaScript, the following sequence of operations take place:

  • A new empty object gets created.
  • The this keyword begins to refer to the new object and it becomes the current instance object.
    This ํ‚ค์›Œ๋“œ๋Š” ์ƒˆ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ํ˜„์žฌ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๊ฐ€ ๋œ๋‹ค.
  • The new object is then returned as the retunr value of the construcotr.

When the this keyword is used in a constructor, it referes to the newly created object:

A constructor can also have parameters.

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