Skip to content

Commit 537e9a0

Browse files
committed
tweaking some wording around integer-looking strings as property names, to be more precise (per #1791)
1 parent 0d68246 commit 537e9a0

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

objects-classes/ch1.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,25 @@ myObj = {
125125

126126
### Property Names
127127

128-
Property names in object literals are almost always treated/coeced as string values. One exception to this is for numeric (or "numeric looking") property "names":
128+
Property names in object literals are almost always treated/coeced as string values. One exception to this is for integer (or "integer looking") property "names":
129129

130130
```js
131131
anotherObj = {
132-
42: "<-- this property name will remain a number",
133-
"41": "<-- this property name will be coerced to a number",
134-
true: "<-- this property name will be converted to a string",
135-
myObj: "<-- ...and so will this one"
132+
42: "<-- this property name will be treated as an integer",
133+
"41": "<-- ...and so will this one",
134+
135+
true: "<-- this property name will be treated as a string",
136+
[myObj]: "<-- ...and so will this one"
136137
};
137138
```
138139

139-
The `42` property name will remain a number, and the `"41"` string value will be coerced to a numeric property name since it *looks like* a number. By contrast, the `true` value will become the string property name `"true"`, and the `myObj` identifier reference will coerce the object's value to a string (generally the default `"[object Object]"`).
140+
The `42` property name will be treated as an integer property name (aka, index); the `"41"` string value will also be treated as such since it *looks like* an integer. By contrast, the `true` value will become the string property name `"true"`, and the `myObj` identifier reference, *computed* via the surrounding `[ .. ]`, will coerce the object's value to a string (generally the default `"[object Object]"`).
140141

141142
| WARNING: |
142143
| :--- |
143-
| If you need to actually use an object as a key/property name, never rely on this string coercion; its behavior is surprising and almost certainly not what's expected, so program bugs are likely to occur. Instead, use a more specialized data structure, called a `Map` (added in ES6), where objects used as property "names" are left as-is instead of being coerced to a string value. |
144+
| If you need to actually use an object as a key/property name, never rely on this computed string coercion; its behavior is surprising and almost certainly not what's expected, so program bugs are likely to occur. Instead, use a more specialized data structure, called a `Map` (added in ES6), where objects used as property "names" are left as-is instead of being coerced to a string value. |
144145

145-
You can also *compute* the **property name** (distinct from computing the property value) at the time of object literal definition:
146+
As with with `[myObj]` above, you can *compute* any **property name** (distinct from computing the property value) at the time of object literal definition:
146147

147148
```js
148149
anotherObj = {

objects-classes/ch2.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ myList[0]; // 23
148148
myList[1]; // 42
149149
```
150150

151-
Recall that any string property name on an object that "looks like" a number -- is able to be validly coerced to a string -- will actually be treated like a number. The same goes for arrays. You should always use `42` as a numeric index (aka, property name), but if you use the string `"42"`, JS will coerce that to a number for you.
151+
Recall that any string property name on an object that "looks like" an integer -- is able to be validly coerced to a numeric integer -- will actually be treated like an integer property (aka, integer index). The same goes for arrays. You should always use `42` as an integer index (aka, property name), but if you use the string `"42"`, JS will assume you meant that as an integer and do that for you.
152152

153153
```js
154-
// "2" works as an index here, but it's not advised
154+
// "2" works as an integer index here, but it's not advised
155155
myList["2"]; // 109
156156
```
157157

158-
One exception to the "no named properties on arrays" rule is that all arrays automatically expose a `length` property, which is automatically kept updated with the "length" of the array.
158+
One exception to the "no named properties on arrays" *rule* is that all arrays automatically expose a `length` property, which is automatically kept updated with the "length" of the array.
159159

160160
```js
161161
myList = [ 23, 42, 109 ];

0 commit comments

Comments
 (0)