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: objects-classes/ch1.md
+9-8Lines changed: 9 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,24 +125,25 @@ myObj = {
125
125
126
126
### Property Names
127
127
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":
129
129
130
130
```js
131
131
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"
136
137
};
137
138
```
138
139
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]"`).
140
141
141
142
| WARNING: |
142
143
| :--- |
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. |
144
145
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:
Copy file name to clipboardExpand all lines: objects-classes/ch2.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,14 +148,14 @@ myList[0]; // 23
148
148
myList[1]; // 42
149
149
```
150
150
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.
152
152
153
153
```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
155
155
myList["2"]; // 109
156
156
```
157
157
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.
0 commit comments