Skip to content

Commit 51c4da3

Browse files
cynthiatorgo
andauthored
Add guidance for attributes vs methods. Resolves #70. (#350)
* Add guidance for attributes vs methods. Resolves #70. * Update index.bs Co-authored-by: Daniel Appelquist <[email protected]> * Update index.bs Co-authored-by: Daniel Appelquist <[email protected]> * Update index.bs * Update index.bs * fixup! Add guidance for attributes vs methods. Resolves #70. Co-authored-by: Daniel Appelquist <[email protected]>
1 parent 973be71 commit 51c4da3

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

index.bs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,47 @@ See also:
13491349

13501350
* [[#attributes-like-data]]
13511351

1352+
<h3 id="attributes-vs-methods">Use attributes or methods appropriately</h3>
1353+
1354+
Sometimes it is unclear
1355+
whether to use an attribute or a method.
1356+
1357+
- Attribute getters must not have any (observable) side effects.
1358+
If you have expected side effects, use a method.
1359+
1360+
- Attribute getters should not perform any blocking operations.
1361+
If a getter requires performing a blocking operation,
1362+
it should be a method.
1363+
1364+
- If the underlying object has not changed,
1365+
attribute getters should return
1366+
the same object each time it is called.
1367+
This means `obj.attribute === obj.attribute` must always hold.
1368+
Returning a new value from an attribute getter
1369+
each time is not allowed.
1370+
If this does not hold, the getter should be a method.
1371+
1372+
Note: An antipattern example of a blocking operation is with getters like offsetTop performing layout.
1373+
1374+
For attributes, whenever possible,
1375+
preserve values given to the setter
1376+
for return from the getter.
1377+
That is, given `obj.attribute = x`,
1378+
a subsequent `obj.attribute === x` should be true.
1379+
(This will not always be the case,
1380+
e.g., if a normalization or type conversion step is necessary,
1381+
but should be held as a goal for normal code paths.)
1382+
1383+
The object you want to return may be [live or static](#js-rtc).
1384+
This means:
1385+
1386+
- If live, then return the same object each time,
1387+
until a state change requires a different object to be returned.
1388+
This can be returned from either an attribute or a method.
1389+
1390+
- If static, then return a new object each time.
1391+
In which case, this should be be a method.
1392+
13521393
<h3 id="prefer-dict-to-bool">Prefer dictionary arguments over primitive arguments</h3>
13531394

13541395
API methods should generally use dictionary arguments

0 commit comments

Comments
 (0)