์์ฑ์ ํจ์์ ์ํ ๊ฐ์ฒด ์์ฑ
Object ์์ฑ์ ํจ์
new์ฐ์ฐ์์ ํจ๊ปObject์์ฑ์ ํจ์๋ฅผ ํธ์ถํ๋ฉด ๋น ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ๋ฐํํจObject์์ฑ์ ํจ์ ์ด์ธ์๋String,Number,Boolean,Function,Array,Date,RegExp,Promise๋ฑ ๋นํธ์ธ ์์ฑ์ ํจ์๋ฅผ ์ ๊ณต
// ๋น ๊ฐ์ฒด์ ์์ฑ
const person = new Object();
// ํ๋กํผํฐ ์ถ๊ฐ
person.name = 'Lee';
person.sayHello = function () {
console.log('Hi! My name is ' + this.name);
};
console.log(person); // {name: "Lee", sayHello: ฦ}
person.sayHello(); // Hi! My name is Lee
// Function ์์ฑ์ ํจ์์ ์ํ Function ๊ฐ์ฒด(ํจ์) ์์ฑ
const func = new Function('x', 'return x * x');
console.log(typeof func); // function
console.dir(func); // ฦ anonymous(x)
// Array ์์ฑ์ ํจ์์ ์ํ Array ๊ฐ์ฒด(๋ฐฐ์ด) ์์ฑ
const arr = new Array(1, 2, 3);
console.log(typeof arr); // object
console.log(arr); // [1, 2, 3]
// RegExp ์์ฑ์ ํจ์์ ์ํ RegExp ๊ฐ์ฒด(์ ๊ท ํํ์) ์์ฑ
const regExp = new RegExp(/ab+c/i);
console.log(typeof regExp); // object
console.log(regExp); // /ab+c/i
// Date ์์ฑ์ ํจ์์ ์ํ Date ๊ฐ์ฒด ์์ฑ
const date = new Date();
console.log(typeof date); // object
console.log(date); // Mon May 04 2020 08:36:33 GMT+0900 (๋ํ๋ฏผ๊ตญ ํ์ค์)
์์ฑ์ ํจ์
- ๋์ผํ ํ๋กํผํฐ๋ฅผ ๊ฐ๋ ๊ฐ์ฒด๋ฅผ ์ฌ๋ฌ๊ฐ ์์ฑํ๋ ๊ฒฝ์ฐ ๊ฐ์ฒด ๋ฆฌํฐ๋ด ๋ฐฉ์์ด ๋นํจ์จ์ (์ฝ๋ ์ค๋ณต)
- ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐ๊ฐ ๋์ผํ ๊ฐ์ฒด ์ฌ๋ฌ ๊ฐ๋ฅผ ๊ฐํธํ๊ฒ ์์ฑ ๊ฐ๋ฅ
// ์์ฑ์ ํจ์
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
this๋ ๊ฐ์ฒด ์์ ์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํ ์๊ธฐ ์ฐธ์กฐ ๋ณ์
- js ์์๋ ์ผ๋ฐ ํจ์์ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ์์ฑ์ ํจ์ ์ ์
new์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ๋ฉด ํด๋น ํจ์๋ ์์ฑ์ ํจ์๋ก ๋์ (new์์ผ๋ฉด ์ผ๋ฐ ํจ์)
์์ฑ์ ํจ์ ์ธ์คํด์ค ์์ฑ ๊ณผ์
- ์์ฑ์ ํจ์ ์ญํ : ์ธ์คํด์ค ์์ฑ, ์์ฑ๋ ์ธ์คํด์ค ์ด๊ธฐํ
- ์์ฑ์ ํจ์ ์์ ์ธ์คํด์ค ๊ฐ ๋ฐํํ์ง ์์๋ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ด ์๋ฌต์ ์ผ๋ก ์ธ์คํด์ค ์์ฑํ๊ณ ๋ฐํ
- ์์ฑ ๊ณผ์
- ์ธ์คํด์ค ์์ฑ๊ณผ this๋ฐ์ธ๋ฉ
๋ฐ์ธ๋ฉ ์ด๋? ์๋ณ์์ ๊ฐ์ ์ฐ๊ฒฐํ๋ ๊ณผ์ , this ๋ฐ์ธ๋ฉ์ด๋ this๊ฐ ๊ฐ๋ฆฌํฌ ๊ฐ์ฒด๋ฅผ ๋ฐ์ธ๋ฉ ํ๋ ๊ฒ
๋ฐํ์ ์ด์ ์ ์๋ฌต์ ์ผ๋ก ๋น ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ณ this์ ๋ฐ์ธ๋ฉ
์ธ์คํด์ค ์ด๊ธฐํ
์ฝ๋ ์คํ ์ ์ด๊ธฐํ ๋จ
์ธ์คํด์ค ๋ฐํ
๋ฐ์ธ๋ฉ ๋ this๊ฐ ์๋ฌต์ ์ผ๋ก ๋ฐํ
๋ช ์์ ์ผ๋ก ๊ฐ์ฒดreturnthis ๋ฐํ ๋ฌด์๋จ
๋ด๋ถ ๋ฉ์๋ [[ Call ]] , [[ Construct ]]
- ํจ์๋ ๊ฐ์ฒด์ด๋ฏ๋ก ๋ด๋ถ ์ฌ๋กฏ๊ณผ ๋ด๋ถ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
- ํจ์์ ๊ฐ์ฒด ๋ค๋ฅธ์ : ์ผ๋ฐ ๊ฐ์ฒด๋ ํธ์ถ ๋ถ๊ฐ๋ฅ, ํจ์ ๊ฐ์ฒด๋ ํธ์ถ ๊ฐ๋ฅ
- ํจ์ ๊ฐ์ฒด๋ ์ผ๋ฐ ๊ฐ์ฒด์ ๋ด๋ถ์ฌ๋กฏ, ๋ด๋ถ ๋ฉ์๋์ ํจ์ ๊ฐ์ฒด๋ฅผ ์ํ ๋ด๋ถ์ฌ๋กฏ๊ณผ ๋ด๋ถ๋ฉ์๋๋ฅผ ์ถ๊ฐ๋ก ๊ฐ์ง๊ณ ์์
- [[ Environment ]] , [[ Formal Parameters ]] : ํจ์ ๊ฐ์ฒด ์ํ ๋ด๋ถ ์ฌ๋กฏ
- [[ Call ]], [[ Construct ]] : ํจ์ ๊ฐ์ฒด ์ํ ๋ด๋ถ ๋ฉ์๋
- ๋ชจ๋ ํจ์ ๊ฐ์ฒด๋ ๋ด๋ถ ๋ฉ์๋ [[ Call ]] ์ ๊ฐ์ง๊ณ ์์ผ๋ฏ๋ก ํธ์ถ ๊ฐ๋ฅ
- ๋ชจ๋ ํจ์ ๊ฐ์ฒด๊ฐ [[ Construct ]] ๊ฐ์ง์ง๋ ์์ (= ์์ฑ์ ์ผ ์ ์๊ณ ์๋ ์ ์๋ค.)
- ํจ์ ํธ์ถ ์ [[ Call ]] ํธ์ถ,
new๋ก ์์ฑ์ ํธ์ถ ์ [[ Construct ]] ํธ์ถ
construct, non-construct ๊ตฌ๋ถ
- ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ด ํจ์ ์ ์๋ฅผ ํ๊ฐํ์ฌ ๊ตฌ๋ถ
- construct : ํจ์ ์ ์ธ๋ฌธ, ํจ์ ํํ์, ํด๋์ค (= ์ผ๋ฐ ํจ์)
- non-construct : ๋ฉ์๋, ํ์ดํ ํจ์
- ๋ฉ์๋๋
ES6์ ๋ฉ์๋ ์ถ์ฝ ํํ๋ง ๋ฉ์๋๋ก ์ธ์ ํฉ๋๋ค.
new ์ฐ์ฐ์
new์ฐ์ฐ์์ ํจ๊ป ํจ์ ํธ์ถ ์ ๋ด๋ถ ๋ฉ์๋ [[ Construct ]] ํธ์ถ ๋จnew์์ด ์์ฑ์ ํจ์ ํธ์ถ ์ [[ Call ]]์ด ํธ์ถ ๋จ- ์์ฑ์ ํจ์๋ฅผ [[ Call ]] ๋ก ํธ์ถ ์ ํจ์ ๋ด๋ถ์
this๋ ์ ์ญ ๊ฐ์ฒดwindow๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
// new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ์ง ์์ผ๋ฉด ์์ฑ์ ํจ์๋ก ๋์ํ์ง ์๋๋ค.
// ์ฆ, ์ผ๋ฐ ํจ์๋ก์ ํธ์ถ๋๋ค.
const circle3 = Circle(15);
// ์ผ๋ฐ ํจ์๋ก์ ํธ์ถ๋ Circle์ ๋ฐํ๋ฌธ์ด ์์ผ๋ฏ๋ก ์๋ฌต์ ์ผ๋ก undefined๋ฅผ ๋ฐํํ๋ค.
console.log(circle3); // undefined
// ์ผ๋ฐ ํจ์๋ก์ ํธ์ถ๋ Circle๋ด์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
console.log(radius); // 15
new.target
- ์์ฑ์ ํจ์๊ฐ
new์์ด ํธ์ถ ๋๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ์ํด ES6์์ ๋์ ๋ ํ๋กํผํฐ, ๋ฉํ ํ๋กํผํฐ๋ผ๊ณ ๋ถ๋ฆ new์ฐ์ฐ์์ ํจ๊ป ํจ์ ํธ์ถ ์ ํจ์ ๋ด๋ถ์ new.target์ ์๊ธฐ ์์ ๊ฐ๋ฆฌํจ๋ค. ๋ฐ๋๋undefined- ์ค์ฝํ ์ธ์ดํ ์์ฑ์ ํจํด
// Scope-Safe Constructor Pattern
function Circle(radius) {
// ์์ฑ์ ํจ์๊ฐ new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถ๋๋ฉด ํจ์์ ์ ๋์์ ๋น ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ
// this์ ๋ฐ์ธ๋ฉํ๋ค. ์ด๋ this์ Circle์ ํ๋กํ ํ์
์ ์ํด ์ฐ๊ฒฐ๋๋ค.
// ์ด ํจ์๊ฐ new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถ๋์ง ์์๋ค๋ฉด ์ด ์์ ์ this๋ ์ ์ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
// ์ฆ, this์ Circle์ ํ๋กํ ํ์
์ ์ํด ์ฐ๊ฒฐ๋์ง ์๋๋ค.
if (!(this instanceof Circle)) {
// new ์ฐ์ฐ์์ ํจ๊ป ํธ์ถํ์ฌ ์์ฑ๋ ์ธ์คํด์ค๋ฅผ ๋ฐํํ๋ค.
return new Circle(radius);
}
this.radius = radius;
this.getDiameter = function () {
return 2 * this.radius;
};
}
// new ์ฐ์ฐ์ ์์ด ์์ฑ์ ํจ์๋ฅผ ํธ์ถํ์ฌ๋ ์์ฑ์ ํจ์๋ก์ ํธ์ถ๋๋ค.
const circle = Circle(5);
console.log(circle.getDiameter()); // 10
์ฐธ๊ณ
- ๋นํธ์ธ ๊ฐ์ฒด ์ค Object, Function ์์ฑ์ ํจ์๋
new์์ด๋ ๊ฐ์ ๊ฒฐ๊ณผ ๋ฐํ - String, Number, Boolean์
newํค์๋ ์์ผ๋ฉด ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฆ
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
const str = new String(123);
console.log(str, typeof str); // String {'123'} 'object'
const num = new Number('123');
console.log(num, typeof num); // Number {123} 'object'
const bool = new Boolean('true');
console.log(bool, typeof bool); // Boolean {true} 'object''javascript > ๐ study' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [3์ฃผ์ฐจ ์คํฐ๋]19์ฅ-ํ๋กํ ํ์ (0) | 2022.02.14 |
|---|---|
| [3์ฃผ์ฐจ ์คํฐ๋]18์ฅ-ํจ์์ ์ผ๊ธ๊ฐ์ฒด (0) | 2022.02.14 |
| [3์ฃผ์ฐจ ์คํฐ๋] 16์ฅ ํ๋กํผํฐ, ์ดํธ๋ฆฌ๋ทฐํธ (0) | 2022.02.14 |
| [2์ฃผ์ฐจ ์คํฐ๋]15์ฅ-let const ๋ธ๋ก ์ค์ฝํ (0) | 2022.02.06 |
| [2์ฃผ์ฐจ ์คํฐ๋]14์ฅ-์ ์ญ๋ณ์ ๋ฌธ์ ์ (0) | 2022.02.06 |
๋๊ธ