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
The`instanceof`operator allows to check whether an object belongs to a certain class. It also takes inheritance into account.
3
+
`instanceof`연산자는 객체가 특정 클래스에 속해 있는지 없는지를 검사합니다. 또한, 상속 관계에 있는지 확인합니다.
4
4
5
-
Such a check may be necessary in many cases, here we'll use it for building a*polymorphic* function, the one that treats arguments differently depending on their type.
5
+
확인하는 기능은 많은 경우에서 필수적인데, 여기에 매개변수의 타입때문에 처리하는 법이 달라지는*다형적인* 함수를 빌드하기 위해 사용할 것입니다.
6
6
7
-
##Theinstanceofoperator[#ref-instanceof]
7
+
##instanceof연산자[#ref-instanceof]
8
8
9
-
The syntax is:
9
+
문법은 아래와 같습니다.
10
10
```js
11
11
objinstanceof Class
12
12
```
13
13
14
-
It returns`true` if`obj` belongs to the`Class` or a class inheriting from it.
14
+
`obj`가`Class`에 속해 있거나 상속하고 있다면, true를 반환합니다.
15
15
16
-
For instance:
16
+
예시:
17
17
18
18
```js run
19
19
classRabbit {}
20
20
let rabbit=newRabbit();
21
21
22
-
//is it an object of Rabbit class?
22
+
//rabbit이 Rabbit class에 속해있는지 물어봅니다.
23
23
*!*
24
24
alert( rabbitinstanceof Rabbit );// true
25
25
*/!*
26
26
```
27
27
28
-
It also works with constructor functions:
28
+
instanceof는 생성자 함수에서도 동작합니다.
29
29
30
30
```js run
31
31
*!*
32
-
//instead of class
32
+
//클래스 대신 함수를 사용하였습니다.
33
33
functionRabbit() {}
34
34
*/!*
35
35
36
36
alert(newRabbit()instanceof Rabbit );// true
37
37
```
38
38
39
-
...And with built-in classes like`Array`:
39
+
또한,`Array` 같은 클래스의 내장함수로써의 기능도 있습니다.
40
40
41
41
```js run
42
42
let arr= [1,2,3];
43
43
alert( arrinstanceofArray );// true
44
44
alert( arrinstanceofObject );// true
45
45
```
46
46
47
-
Please note that`arr` also belongs to the`Object`class. That's because`Array` prototypically inherits from`Object`.
47
+
`arr`은 또한,`Object`class에 속해있다는 것을 기억하세요.`Array`는 원래`Object`를 상속하고 있습니다.
48
48
49
-
Normally,`instanceof`operator examines the prototype chain for the check. We can also set a custom logic in the static method`Symbol.hasInstance`.
49
+
보통,`instanceof`연산자는 프로토타입 체인의 확인을 검사해줍니다. 또한, 정적 메서드`Symbol.hasInstance`로 커스텀 로직을 설정할수 있습니다.
50
50
51
-
The algorithm of`obj instanceof Class` works roughly as follows:
51
+
`obj instanceof Class`의 알고리즘은 대략적으로 다음과 같이 동작합니다.
52
52
53
-
1.If there's a static method`Symbol.hasInstance`, then just call it:`Class[Symbol.hasInstance](obj)`.It should return either`true`or`false`, and we're done. That's how we can customize the behavior of`instanceof`.
53
+
1.정적 메서드`Symbol.hasInstance`가 있다고 가정한다면, 그저 호출하면 됩니다.`Class[Symbol.hasInstance](obj)`.결과는`true`나`false`값을 반환할 것입니다. 다음은`instanceof`를 어떻게 커스텀 하는지에 대한 예시입니다.
54
54
55
-
For example:
55
+
예시:
56
56
57
57
```js run
58
-
// setup instanceOf check that assumes that
59
-
//anything withcanEatproperty is an animal
58
+
59
+
//instanceOf는canEat프로퍼티가 aniaml인지 확인하는 것입니다.
60
60
classAnimal {
61
61
static [Symbol.hasInstance](obj) {
62
62
if (obj.canEat)returntrue;
@@ -65,24 +65,24 @@ The algorithm of `obj instanceof Class` works roughly as follows:
65
65
66
66
let obj= { canEat:true };
67
67
68
-
alert(objinstanceof Animal);// true:Animal[Symbol.hasInstance](obj) is called
68
+
alert(objinstanceof Animal);// true:Animal에서 저희가 커스터마이즈한Symbol.hasInstance가 호출 되었기 때문에 true를 리턴했습니다.
69
69
```
70
70
71
-
2.Most classesdo not have`Symbol.hasInstance`.In that case, the standard logic is used:`obj instanceOf Class` checks whether`Class.prototype` equals to oneof prototypesin the`obj` prototype chain.
71
+
2.대부분 클래스는`Symbol.hasInstance`를 가지고 있지 않습니다. 이러한 경우, 일반적인 로직이 사용될 것입니다.`obj instanceOf Class`는`Class.prototype`이`obj` 프로토 체인 내부의 프로토타입 중 하나와 같은지 확인합니다.
Here's the illustration of what`rabbit instanceof Animal` compares with`Animal.prototype`:
102
+
여기에`rabbit instanceof Animal`의 무엇과`Animal.prototype`을 비교하는 지에 대한 그림이 있습니다.
103
103
104
104

105
105
106
-
By the way, there's also a method[objA.isPrototypeOf(objB)](mdn:js/object/isPrototypeOf), that returns`true`if`objA` is somewherein the chainof prototypesfor`objB`.So the testof`obj instanceof Class` can be rephrased as`Class.prototype.isPrototypeOf(obj)`.
106
+
그런데,`objA`가`objB`의 프로토타입의 체인 어딘가에 있다면,`true`를 리턴하는[objA.isPrototypeOf(objB)](mdn:js/object/isPrototypeOf)라는 메서드가 있습니다.
107
107
108
-
It's funny, but the`Class`constructor itself does not participate in the check! Only the chain of prototypes and`Class.prototype` matters.
108
+
`Class`생성자 그 자체는 확인할 수 없지만, 오직 프르토타입 체인과`Class.prototype`은 매우 중요하므로 확인해야 합니다.
109
109
110
-
That can lead to interesting consequences when`prototype` property is changed after the object is created.
110
+
언제`prototype`프로퍼티가 객체가 생성된 후에 변화되는지에 따라 흥미로운 결과를 이끌어 낼수 있습니다.
111
111
112
-
Like here:
112
+
아래와 같이 말이죠.
113
113
114
114
```js run
115
115
function Rabbit() {}
116
116
let rabbit = new Rabbit();
117
117
118
-
//changed the prototype
118
+
//프로토타입을 변형했습니다.
119
119
Rabbit.prototype = {};
120
120
121
-
// ...not a rabbit any more!
121
+
// ...더이상 Rabbit이 아닙니다!
122
122
*!*
123
123
alert( rabbit instanceof Rabbit ); // false
124
124
*/!*
125
125
```
126
126
127
-
##Bonus:Object.prototype.toString for the type
127
+
##보너스: 타입을 위한Object.prototype.toString
128
128
129
-
We already know that plain objects are converted to string as`[object Object]`:
129
+
이미 여러분들은 평범한 객체들이`[object Object]`의 문자열로 변환되는 것에 대해 알고 있습니다.
130
130
131
131
```js run
132
132
let obj = {};
133
133
134
134
alert(obj); // [object Object]
135
-
alert(obj.toString()); //the same
135
+
alert(obj.toString()); //같습니다.
136
136
```
137
137
138
-
That's their implementationof`toString`.But there's a hidden feature that makes `toString` actually much more powerful than that. We can use it as an extended `typeof` and an alternative for`instanceof`.
138
+
그것은 바로`toString`으로 구현되어 있습니다. 그러나`toString`은 실질적으로 그것이 가진 기능보다 더 강력하게 만들어 줄 수 있는 몇 가지 숨겨진 특징들이 있습니다. 확장된 기능으로써`typeof`을 사용할수 있는데, 이것은`instanceof` 대신 사용할 수 있습니다.
139
139
140
-
Sounds strange? Indeed. Let's demystify.
140
+
이상하게 들리나요? 그럼 미스터리로 두죠.
141
141
142
-
By [specification](https://tc39.github.io/ecma262/#sec-object.prototype.tostring), the built-in `toString` can be extracted from the object and executed in the context of any other value. And its result depends on that value.
142
+
[이곳](https://tc39.github.io/ecma262/#sec-object.prototype.tostring)에 명시되어 있듯이, 객체와 실행중인 다른 값의 컨텍스트로부터 내장함수 `toString`을 사용하여 추출할 수 있습니다. 그리고 그 결과는 그 객체가 가진 값에 의존합니다.
143
143
144
-
-For a number, it will be`[object Number]`
145
-
-For a boolean, it will be`[object Boolean]`
146
-
-For`null`:`[object Null]`
147
-
-For`undefined`:`[object Undefined]`
148
-
-For arrays:`[object Array]`
149
-
-...etc (customizable).
144
+
-숫자의 경우, 그것은`[object Number]`가 될 것입니다.
145
+
-불린의 경우, 그것은`[object Boolean]`이 될 것입니다.
146
+
-null의 경우, 그것은`[object Null]`가 될 것입니다.
147
+
-`undefined`의 경우, 그것은`[object Undefined]`가 될 것입니다.
148
+
-배열의 경우, 그것은`[object Array]`가 될 것입니다.
149
+
-그외에 여러가지가 다양하게 있는데, 그것에 따라 맞춰집니다.
150
150
151
-
Let's demonstrate:
151
+
그럼 같이 한번 해결해볼까요?
152
152
153
153
```js run
154
-
//copytoStringmethod into a variable for convenience
Here we used [call](mdn:js/function/call) as described in the chapter [](info:call-apply-decorators) to execute the function`objectToString` in the context`this=arr`.
163
+
이번 챕터에서 [](info:call-apply-decorators)에 서술되어 있는 [call](mdn:js/function/call)는 함수`objectToString`은`this=arr` 컨텍스트에 실행하기 위해서 사용했습니다.
164
164
165
-
Internally, the`toString`algorithm examines`this` and returns the corresponding result. More examples:
165
+
내부적으로,`toString`알고리즘은`this`를 검사하고, 일치하는 결과를 반환합니다. 더 많은 예시를 살펴봅시다.