Skip to content

Commit 97a5c22

Browse files
Update introduction.md
1 parent 61efdc4 commit 97a5c22

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

exercises/concept/recycling-robot/.docs/introduction.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Introduction
22

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.
44

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.
66

77
```exercism/note
88
Javascript's type checking mechanisms can be somewhat unreliable.
@@ -12,8 +12,9 @@ For better type safety and stronger types, you should probably use TypeScript, a
1212

1313
## The `typeof` operator
1414

15-
The `typeof` operator returns the type of its input.
16-
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"`.
1718

1819
```javascript
1920
typeof undefined;
@@ -40,23 +41,25 @@ typeof { city: 'Stockholm', country: 'Sweden' };
4041
// => "object"
4142
```
4243

43-
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"`].
4445

4546
## The `instanceof` operator
4647

4748
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].
4950
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.
5152

5253
```javascript
5354
class Beverage {
5455
// ...
5556
}
57+
5658
// The Coffee class is a child of the Beverage class.
5759
class Coffee extends Beverage {
5860
// ...
5961
}
62+
6063
const java = new Coffee();
6164

6265
java instanceof Coffee;
@@ -69,27 +72,40 @@ java instanceof Beverage;
6972
```exercism/advanced
7073
The `Array` class has a method called `Array.isArray()` that checks if its argument is an array.
7174
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.
7376
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.
7578
`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+
```
7689
```
7790
7891
## The `in` operator
7992
8093
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`.
8296
8397
```javascript
8498
class Coffee {
8599
constructor() {
86100
this.temperature = 'hot';
87101
this.isDarkMatter = undefined;
88102
}
103+
89104
coolDown() {
90105
this.temperature = 'warm';
91106
}
92107
}
108+
93109
const espresso = new Coffee();
94110
95111
'temperature' in espresso;
@@ -103,26 +119,29 @@ const espresso = new Coffee();
103119
```
104120

105121
````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+
107124
```javascript
108125
"coolDown" in espresso
109126
// => true
110127
111128
"constructor" in espresso
112129
// => true
113130
```
114-
To avoid this, use the hasOwnProperty() method.
131+
132+
To avoid this, use `Object.hasOwn()` instead
115133
````
116134

117135
## The `Object.hasOwn()` function
118136

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).
120138

121139
```javascript
122140
class Coffee {
123141
constructor() {
124142
this.temperature = 'hot';
125143
}
144+
126145
coolDown() {
127146
this.temperature = 'warm';
128147
}

0 commit comments

Comments
 (0)