You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/06-constructor-new/article.md
-24Lines changed: 0 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,6 @@
1
1
# new 연산자와 생성자 함수
2
2
3
-
<<<<<<< HEAD
4
3
객체 리터럴 `{...}` 을 사용하면 객체를 쉽게 만들 수 있습니다. 그런데 개발을 하다 보면 유사한 객체를 여러 개 만들어야 할 때가 생기곤 합니다. 복수의 사용자, 메뉴 내 다양한 아이템을 객체로 표현하려고 하는 경우가 그렇죠.
5
-
=======
6
-
The regular `{...}` syntax allows us to create one object. But often we need to create many similar objects, like multiple users or menu items and so on.
7
-
>>>>>>> upstream/master
8
4
9
5
`'new'` 연산자와 생성자 함수를 사용하면 유사한 객체 여러 개를 쉽게 만들 수 있습니다.
10
6
@@ -68,17 +64,10 @@ let user = {
68
64
69
65
생성자의 의의는 바로 여기에 있습니다. 재사용할 수 있는 객체 생성 코드를 구현하는 것이죠.
70
66
71
-
<<<<<<< HEAD
72
67
잠깐! 모든 함수는 생성자 함수가 될 수 있다는 점을 잊지 마시기 바랍니다. `new`를 붙여 실행한다면 어떤 함수라도 위에 언급된 알고리즘이 실행됩니다. 이름의 '첫 글자가 대문자'인 함수는 `new`를 붙여 실행해야 한다는 점도 잊지 마세요. 공동의 약속이니까요.
73
68
74
69
````smart header="new function() { ... }"
75
70
재사용할 필요가 없는 복잡한 객체를 만들어야 한다고 해봅시다. 많은 양의 코드가 필요할 겁니다. 이럴 땐 아래와 같이 코드를 익명 생성자 함수로 감싸주는 방식을 사용할 수 있습니다.
76
-
=======
77
-
Let's note once again -- technically, any function (except arrow functions, as they don't have `this`) can be used as a constructor. It can be run with `new`, and it will execute the algorithm above. The "capital letter first" is a common agreement, to make it clear that a function is to be run with `new`.
78
-
79
-
````smart header="new function() { ... }"
80
-
If we have many lines of code all about creation of a single complex object, we can wrap them in an immediately called constructor function, like this:
81
-
>>>>>>> upstream/master
82
71
83
72
```js
84
73
// create a function and immediately call it with new
@@ -92,11 +81,7 @@ let user = new function() {
92
81
};
93
82
```
94
83
95
-
<<<<<<< HEAD
96
84
위 생성자 함수는 익명 함수이기 때문에 어디에도 저장되지 않습니다. 처음 만들 때부터 단 한 번만 호출할 목적으로 만들었기 때문에 재사용이 불가능합니다. 이렇게 익명 생성자 함수를 이용하면 재사용은 막으면서 코드를 캡슐화 할 수 있습니다.
97
-
=======
98
-
This constructor can't be called again, because it is not saved anywhere, just created and called. So this trick aims to encapsulate the code that constructs the single object, without future reuse.
99
-
>>>>>>> upstream/master
100
85
````
101
86
102
87
## new.target과 생성자 함수
@@ -107,11 +92,7 @@ This constructor can't be called again, because it is not saved anywhere, just c
107
92
108
93
`new.target` 프로퍼티를 사용하면 함수가 `new`와 함께 호출되었는지 아닌지를 알 수 있습니다.
109
94
110
-
<<<<<<< HEAD
111
95
일반적인 방법으로 함수를 호출했다면 `new.target`은 undefined를 반환합니다. 반면 `new`와 함께 호출한 경우엔 `new.target`은 함수 자체를 반환해줍니다.
112
-
=======
113
-
It is undefined for regular calls and equals the function if called with `new`:
114
-
>>>>>>> upstream/master
115
96
116
97
```js run
117
98
functionUser() {
@@ -189,13 +170,8 @@ alert( new SmallUser().name ); // 원숭이
189
170
190
171
`return`문이 있는 생성자 함수는 거의 없습니다. 여기선 튜토리얼의 완성도를 위해 특이 케이스를 소개해보았습니다.
0 commit comments