(Map|Set|WeakMap)#set() returns `this` ?

Rick Waldron waldron.rick at gmail.com
Tue Dec 4 14:57:32 PST 2012


On Tue, Dec 4, 2012 at 3:50 PM, Brendan Eich <brendan at mozilla.org> wrote:

> The survey I'd like to see is among underscore, jQuery, and other popular
> libraries (go by NPM dependency frequency and client-side use frequency),
> how do set-like methods work: return-this or return-v?
>
>
underscore and jQuery side with

1. returning the calling object
2. returning a new object of the same "kind", but with the results of the
operation.

jQuery objects themselves are similar to a set, most operations on the set
itself result in returning a new jQuery object (set) that reflects the
results of the operation. As I explained previously, the reason it returns
a new jQuery object is to support the end() and addBack() methods by
storing the original as a property of the current, this that allows either
restoring the original set to it's pre-operation state or adding the
original set to the current set. If this were not an API that we supported,
jQuery could just as easily be a custom wrapper over an ES6 Set.

underscore actually has a special method called _.chain() that returns a
wrapped object that allows for chaining all of the underscore APIs—until
value() is called, which closely matches the examples I gave yesterday.
underscore has a _.defaults()  function that returns the mutated object
after applying a default set of properties and values (similar to
_.extend()). There is no analogous Set api in underscore.


for npm in order of most dependencies:

1. underscore (see above).

2. request.

   - Any API that "sets" something, returns this (
   https://github.com/mikeal/request/blob/master/main.js#L735-L758 )
   - There is a lib that Mikeal also wrote "form-data" that has an append
   method that does _not_ return this

3. async. Nothing comparable

4. express.

   - app.set, and subsequently all aliases, returns this (
   https://github.com/visionmedia/express/blob/master/lib/application.js#L250-L261
   )
   - response.set, and subsequently all aliases, returns this (
   https://github.com/visionmedia/express/blob/master/lib/response.js#L37-L40
   )

5. optimist.

   - every operation that is not a "get" returns self as this; (
   https://github.com/substack/node-optimist/blob/master/index.js)

6. commander.

   - nearly every operation that is not a "get" returns this; (
   https://github.com/visionmedia/commander.js/blob/master/index.js)

7. colors

   - setTheme returns the theme that was set

8. uglify. Nothing comparable

9. connect.

   - Too much to review, but it's primary API that "sets" values,  returns
   this (see examples in readme)

10. socket.io

   - Too much to review, nearly everything returns this (
   https://github.com/LearnBoost/socket.io/blob/master/lib/socket.js)


When I was researching whether or not it was worthwhile to pursue this
discussion, I reviewed most of this stuff, as well as web-focused libs.

At the moment I have to head out, so I can't continue to research this, but
I'll gladly spend as many hours as anyone thinks is necessary to prove my
position.

Rick











> This is closer than a people-survey to mapping the cowpath.
>
> /be
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121204/a79e660f/attachment.html>


More information about the es-discuss mailing list