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: index.bs
+25Lines changed: 25 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1190,6 +1190,31 @@ which might be expressed as a bitmask in another language,
1190
1190
use a dictionary object instead.
1191
1191
This object can be passed around as easily as a single bitmask value.
1192
1192
1193
+
<h3 id="properties-vs-methods">Properties vs. Methods</h3>
1194
+
1195
+
Sometimes it is unclear whether to use a property or a method.
1196
+
1197
+
* Property getters must not have any (observable) side effects.
1198
+
If you have expected side effects, use a method.
1199
+
* Property getters are expected to represent the state of a given object.
1200
+
This means a getter should be able to efficiently return a value using the existing state.
1201
+
If a getter does not satisfy this, it should be a method.
1202
+
(A notable failure of the platform in this regard is getters like offsetTop performing layout; do not repeat this mistake.)
1203
+
* If the underlying object has not changed, property getters should return the same object each time it is called.
1204
+
This means <code>obj.property === obj.property</code> must always hold.
1205
+
Returning a new value from a property getter each time is not allowed.
1206
+
If this does not hold, the getter should be a method.
1207
+
1208
+
For properties, whenever possible, preserve values given to the setter for return from the getter. That is,
1209
+
given <code>obj.property = x</code>, a subsequent <code>obj.property === x</code> should be true. (This will not always be the case, e.g., if a normalization or type conversion step is necessary, but should be held as a goal for normal code paths.)
1210
+
1211
+
The object you want to return may be <a href="#live-vs-static">live or static</a>. This means:
1212
+
1213
+
* If live, then return the same object each time, until a state change requires a different object to be returned.
1214
+
This can be returned from either an property or a method.
1215
+
* If static, then return a new object each time.
1216
+
In which case, this should be be a method.
1217
+
1193
1218
<h2 id="event-design">Event Design</h2>
1194
1219
1195
1220
<h3 id="one-time-events">Use promises for one time events</h3>
0 commit comments