Skip to content

Commit b21d67b

Browse files
committed
[new 연산자와 생성자 함수] 충돌 해결 (본문)
1 parent cc9385a commit b21d67b

1 file changed

Lines changed: 0 additions & 24 deletions

File tree

  • 1-js/04-object-basics/06-constructor-new

1-js/04-object-basics/06-constructor-new/article.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# new 연산자와 생성자 함수
22

3-
<<<<<<< HEAD
43
객체 리터럴 `{...}` 을 사용하면 객체를 쉽게 만들 수 있습니다. 그런데 개발을 하다 보면 유사한 객체를 여러 개 만들어야 할 때가 생기곤 합니다. 복수의 사용자, 메뉴 내 다양한 아이템을 객체로 표현하려고 하는 경우가 그렇죠.
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
84

95
`'new'` 연산자와 생성자 함수를 사용하면 유사한 객체 여러 개를 쉽게 만들 수 있습니다.
106

@@ -68,17 +64,10 @@ let user = {
6864

6965
생성자의 의의는 바로 여기에 있습니다. 재사용할 수 있는 객체 생성 코드를 구현하는 것이죠.
7066

71-
<<<<<<< HEAD
7267
잠깐! 모든 함수는 생성자 함수가 될 수 있다는 점을 잊지 마시기 바랍니다. `new`를 붙여 실행한다면 어떤 함수라도 위에 언급된 알고리즘이 실행됩니다. 이름의 '첫 글자가 대문자'인 함수는 `new`를 붙여 실행해야 한다는 점도 잊지 마세요. 공동의 약속이니까요.
7368

7469
````smart header="new function() { ... }"
7570
재사용할 필요가 없는 복잡한 객체를 만들어야 한다고 해봅시다. 많은 양의 코드가 필요할 겁니다. 이럴 땐 아래와 같이 코드를 익명 생성자 함수로 감싸주는 방식을 사용할 수 있습니다.
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
8271
8372
```js
8473
// create a function and immediately call it with new
@@ -92,11 +81,7 @@ let user = new function() {
9281
};
9382
```
9483
95-
<<<<<<< HEAD
9684
위 생성자 함수는 익명 함수이기 때문에 어디에도 저장되지 않습니다. 처음 만들 때부터 단 한 번만 호출할 목적으로 만들었기 때문에 재사용이 불가능합니다. 이렇게 익명 생성자 함수를 이용하면 재사용은 막으면서 코드를 캡슐화 할 수 있습니다.
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
10085
````
10186

10287
## new.target과 생성자 함수
@@ -107,11 +92,7 @@ This constructor can't be called again, because it is not saved anywhere, just c
10792

10893
`new.target` 프로퍼티를 사용하면 함수가 `new`와 함께 호출되었는지 아닌지를 알 수 있습니다.
10994

110-
<<<<<<< HEAD
11195
일반적인 방법으로 함수를 호출했다면 `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
11596

11697
```js run
11798
function User() {
@@ -189,13 +170,8 @@ alert( new SmallUser().name ); // 원숭이
189170

190171
`return`문이 있는 생성자 함수는 거의 없습니다. 여기선 튜토리얼의 완성도를 위해 특이 케이스를 소개해보았습니다.
191172

192-
<<<<<<< HEAD
193173
````smart header="괄호 생략하기"
194174
인수가 없는 생성자 함수는 괄호를 생략해 호출할 수 있습니다.
195-
=======
196-
````smart header="Omitting parentheses"
197-
By the way, we can omit parentheses after `new`:
198-
>>>>>>> upstream/master
199175
200176
```js
201177
let user = new User; // <-- 괄호가 없음

0 commit comments

Comments
 (0)