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 one exception to this is that `typeof null` returns `"object"` for [historical reasons][typeof null is object].
44
+
For [historical reasons][`typeof null` is `"object"`].
44
45
45
46
## The `instanceof` operator
46
47
47
48
For checking the type of an object, you can use the `instanceof` operator.
48
-
It returns a boolean depending on whether the second operand is included in the first operands' [prototype chain][prototype chain].
49
+
It evaluates into a `boolean` depending on whether the second operand is included in the first operands' [prototype chain][prototype chain].
49
50
To clarify, `instanceof` will return whether the first operand is an instance of second operand or one of its child classes.
50
-
`instanceof` only works for compound data types, such as arrays and objects.
51
+
`instanceof` only works on objects.
51
52
52
53
```javascript
53
54
classBeverage {
54
55
// ...
55
56
}
57
+
56
58
// The Coffee class is a child of the Beverage class.
57
59
classCoffeeextendsBeverage {
58
60
// ...
59
61
}
62
+
60
63
constjava=newCoffee();
61
64
62
65
java instanceof Coffee;
@@ -69,27 +72,40 @@ java instanceof Beverage;
69
72
```exercism/advanced
70
73
The `Array` class has a method called `Array.isArray()` that checks if its argument is an array.
71
74
72
-
While `instanceof Array` will not work with an array created in a different `iframe` in a webpage, `Array.isArray()` will.
75
+
While `instanceof Array` will not work with an array created in a different realm such as an `iframe` in a webpage, `Array.isArray()` will.
73
76
74
-
This is because the Array class has a different constructor in each `iframe`, meaning that the function in the prototype chain will be different, causing `instanceof Array` to fail.
77
+
This is because the Array class has a different constructor in each realm, and each `iframe` has its own ream, meaning that the function in the prototype chain will be different, causing `instanceof Array` to fail.
75
78
`Array.isArray()` is capable of ignoring this, and should always be used when possible.
79
+
80
+
It can also survive false positives where an object isn't actually an `Array`, and merely has `Array` in its prototype chain.
81
+
82
+
```javascript
83
+
({ __proto__: Array.prototype }) instanceof Array
84
+
// => true
85
+
86
+
Array.isArray({ __proto__: Array.prototype })
87
+
// => false
88
+
```
76
89
```
77
90
78
91
## The `in` operator
79
92
80
93
The `in` operator returns whether the first operand is a property of the second operand.
81
-
It does not check that the property is defined, a property set to `undefined` will still be detected by `in`.
94
+
It does not check that the property has a defined value.
95
+
A property set to `undefined` will still be detected by `in`.
82
96
83
97
```javascript
84
98
class Coffee {
85
99
constructor() {
86
100
this.temperature = 'hot';
87
101
this.isDarkMatter = undefined;
88
102
}
103
+
89
104
coolDown() {
90
105
this.temperature = 'warm';
91
106
}
92
107
}
108
+
93
109
const espresso = new Coffee();
94
110
95
111
'temperature' in espresso;
@@ -103,26 +119,29 @@ const espresso = new Coffee();
103
119
```
104
120
105
121
````exercism/note
106
-
`in` can be slightly unreliable, as it will return `true` for inherited properties and methods.
122
+
`in` will return `true` for inherited properties and methods.
123
+
107
124
```javascript
108
125
"coolDown" in espresso
109
126
// => true
110
127
111
128
"constructor" in espresso
112
129
// => true
113
130
```
114
-
To avoid this, use the hasOwnProperty() method.
131
+
132
+
To avoid this, use `Object.hasOwn()` instead
115
133
````
116
134
117
135
## The `Object.hasOwn()` function
118
136
119
-
The `Object.hasOwn()` method returns whether the specified object has _its own property_ (not inherited or a method) that matches a string.
137
+
The `Object.hasOwn()` method returns whether the specified object _owns the given property_ (it is not inherited or a method).
0 commit comments