Skip to content

Commit 0e3164c

Browse files
torgocynthiaplinss
authored
Added some text for "Attributes vs Methods" (#262)
* Update index.bs Initital commit with Sangwhan's first text, not incorporating Domenic's comments yet. Co-Authored-By: Sangwhan "fish" Moon <[email protected]> * Fixed markup * fixup! Update index.bs * added link to 5.2 * Update index.html * Update index.bs * Update index.bs Co-authored-by: Peter Linss <[email protected]> * Update index.bs Co-authored-by: Peter Linss <[email protected]> * Update index.bs * remove build output Co-authored-by: Sangwhan "fish" Moon <[email protected]> Co-authored-by: Peter Linss <[email protected]>
1 parent b295eb7 commit 0e3164c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

index.bs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,31 @@ which might be expressed as a bitmask in another language,
11901190
use a dictionary object instead.
11911191
This object can be passed around as easily as a single bitmask value.
11921192

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+
11931218
<h2 id="event-design">Event Design</h2>
11941219

11951220
<h3 id="one-time-events">Use promises for one time events</h3>

0 commit comments

Comments
 (0)