๋ชจ๋ํ ์ ์๋๊ฑฐ ๋ง๋๊ฑฐ๋?
2024/02/229(๋ชฉ) 5:00 PM
17์ฅ ์์ฑ์ ํจ์์ ์ํ ๊ฐ์ฒด ์์ฑ(234.page ~ 246.page)
์์ฑ์ ํจ์๋ 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>
๋ฐ๋ผ์ ์์ฑ์ ํจ์์์ ๋ฌด์ธ๊ฐ๋ฅผ ๋ฐํํ๋ ค๊ณ ํ๋ฉด ์๋๋ค.
์ด๋ ์์ฑ์ ํจ์์ ๊ธฐ๋ณธ ๋์์ ํผ์ํ๋ค.
ํจ์๋ ๊ฐ์ฒด์ด๋ฏ๋ก ์ผ๋ฐ ๊ฐ์ฒด์ ๋์ผํ๊ฒ ๋์ํ ์ ์๋ค.
ํจ์ ๊ฐ์ฒด๋ ์ผ๋ฐ ๊ฐ์ฒด๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ด๋ถ ์ฌ๋กฏ๊ณผ ๋ด๋ถ ๋ฉ์๋๋ฅผ ๋ชจ๋ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ด๋ค.
<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: ๋ฉ์๋(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 ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ๋ ํจ์๋ non-constructor๊ฐ ์๋ constructor์ด์ด์ผ ํ๋ค.
๊ทธ๋ฆฌ๊ณ ์์ฑ์ ํจ์๋ฅผ ์ธ๋๋ ์ผ๋ฐ์ ์ผ๋ก ์ฒซ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๊ธฐ์ ํ๋ ํ์ค์นผ ์ผ์ด์ค๋ก ๋ช ๋ช ํ๋ ๊ฒ ๊ฐ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด์ ์ผ๋ฐ ํจ์์ ๊ตฌ๋ณํ ์ ์๋๋ก ๊ถ์ฅ ํ๋ค.
ํ์ง๋ง ํ์ค์นผ ์ผ์ด์ค ์ปจ๋ฒค์ ์ ์ฌ์ฉํ๋ค ํ๋๋ผ๋ ์ค์๋ ์ธ์ ๋ ๋ฐ์ํ ์ ์๋ค.
์ด๋ฌํ ์ํ์ฑ์ ํํผํ๊ธฐ ์ํด 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
๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ - ์ด์ ๋ชจ ์ง์