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: concepts/type-checking/introduction.md
+36-16Lines changed: 36 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,20 @@
1
1
# About
2
2
3
-
Knowning what type an object has is often very important for code to run smoothly and without errors.
3
+
Knowning what the type of a piece of data is, is often very important for code to run smoothly and without errors.
4
4
5
-
Javascript has several ways to check the type of an object.
5
+
Javascript has several ways to check the type of a value or object.
6
6
7
-
````exercism/note
7
+
```exercism/note
8
8
Javascript's type checking mechanisms can be somewhat unreliable.
9
9
10
-
For better type safety and stronger types, you should probably use TypeScript, a language that builds on JavaScript, but with the type syntax of a static-typed language.```
10
+
For better type safety and stronger types, you should probably use TypeScript, a language that builds on JavaScript, but with the type syntax of a static-typed language.
11
+
```
11
12
12
13
## The `typeof` operator
13
14
14
-
The `typeof` operator returns the type of its input.
15
-
The output is restricted to one of the [primitive data types][primitives], `"function"` or `"object"`.
15
+
The `typeof` operator returns the type of its operand.
16
+
The output is a string matching the name of one of the [primitive data types][primitives], except for `"null"`.
17
+
It can also be `"function"` or `"object"`.
16
18
17
19
```javascript
18
20
typeofundefined;
@@ -37,25 +39,27 @@ typeof [1, 2, 3, 4];
37
39
38
40
typeof { city:'Stockholm', country:'Sweden' };
39
41
// => "object"
40
-
````
42
+
```
41
43
42
-
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"`].
43
45
44
46
## The `instanceof` operator
45
47
46
48
For checking the type of an object, you can use the `instanceof` operator.
47
-
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].
48
50
To clarify, `instanceof` will return whether the first operand is an instance of second operand or one of its child classes.
49
-
`instanceof` only works for compound data types, such as arrays and objects.
51
+
`instanceof` only works on objects.
50
52
51
53
```javascript
52
54
classBeverage {
53
55
// ...
54
56
}
57
+
55
58
// The Coffee class is a child of the Beverage class.
56
59
classCoffeeextendsBeverage {
57
60
// ...
58
61
}
62
+
59
63
constjava=newCoffee();
60
64
61
65
java instanceof Coffee;
@@ -68,27 +72,40 @@ java instanceof Beverage;
68
72
```exercism/advanced
69
73
The `Array` class has a method called `Array.isArray()` that checks if its argument is an array.
70
74
71
-
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.
72
76
73
-
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.
74
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
+
```
75
89
```
76
90
77
91
## The `in` operator
78
92
79
93
The `in` operator returns whether the first operand is a property of the second operand.
80
-
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`.
81
96
82
97
```javascript
83
98
class Coffee {
84
99
constructor() {
85
100
this.temperature = 'hot';
86
101
this.isDarkMatter = undefined;
87
102
}
103
+
88
104
coolDown() {
89
105
this.temperature = 'warm';
90
106
}
91
107
}
108
+
92
109
const espresso = new Coffee();
93
110
94
111
'temperature' in espresso;
@@ -102,26 +119,29 @@ const espresso = new Coffee();
102
119
```
103
120
104
121
````exercism/note
105
-
`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
+
106
124
```javascript
107
125
"coolDown" in espresso
108
126
// => true
109
127
110
128
"constructor" in espresso
111
129
// => true
112
130
```
113
-
To avoid this, use the hasOwnProperty() method.
131
+
132
+
To avoid this, use `Object.hasOwn()` instead
114
133
````
115
134
116
135
## The `Object.hasOwn()` function
117
136
118
-
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