ES accessor usage guidelines (Was: Map/Set.prototype.size)

Yehuda Katz wycats at gmail.com
Tue Oct 16 14:21:18 PDT 2012


Yehuda Katz
(ph) 718.877.1325


On Tue, Oct 16, 2012 at 5:13 PM, Brendan Eich <brendan at mozilla.org> wrote:

> Yehuda Katz wrote:
>
>> Agreed. For example:
>>
>> class Post {
>>
>> }
>>
>> class Comment {
>>
>> }
>>
>> Post.hasMany("comments");
>> Comment.belongsTo("post");
>>
>> let post = new Post()
>> let comment = new Comment();
>>
>> comment.post = post;
>> post.comments //=> [comment]
>>
>
> I'm with Allen: you shoulda used a method!
>

I feel somewhat strongly that this is an appropriate use of a setter. Using
a method would give the API a static language feel for not enough win. In
the case of a well-defined API with well-understood links between objects,
a setter feels right to me.

In fact, the entire point of Object.observe as an API is to produce
side-effects of setting data properties. Making it asynchronous solves the
programming hazards, but doesn't change the larger philosophical point.
Data bindings and similar systems general produce side effects from
setters, and I am ok with that.


>
> I know, this is all allowed, so it will happen. We're talking "Design
> Rules" here, which the language cannot enforce. But really, too much spooky
> setter action at a distance, even for bidirectionally-linked objects.
>
> /be
>
>>
>> This is similar to certain DOM APIs, and my expectation of a hypothetical
>> version of Ember Data in ES6 would work. I don't think there is anything
>> wrong with using an accessor here.
>>
>> Yehuda Katz
>> (ph) 718.877.1325
>>
>>
>> On Tue, Oct 16, 2012 at 4:12 PM, Erik Arvidsson <erik.arvidsson at gmail.com<mailto:
>> erik.arvidsson at gmail.**com <erik.arvidsson at gmail.com>>> wrote:
>>
>>     On Mon, Oct 15, 2012 at 12:23 PM, Brendan Eich
>>     <brendan at mozilla.org <mailto:brendan at mozilla.org>> wrote:
>>     > * get/set accessor may have effects on 'set' (see the DOM) but
>>     only on the
>>     > receiver object (and unobservably, any children that become
>>     garbage, e.g.
>>     > when trimming .length on an array-like).
>>
>>     That is very limiting, even as a guideline. Any time there are two or
>>     more related objects it is very likely that a setter might affect some
>>     other object.
>>
>>     --
>>     erik
>>     ______________________________**_________________
>>     es-discuss mailing list
>>     es-discuss at mozilla.org <mailto:es-discuss at mozilla.org**>
>>     https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>>
>>
>>
>> ______________________________**_________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121016/ee93484e/attachment-0001.html>


More information about the es-discuss mailing list