Map/Set.prototype.size

# David Bruant (12 years ago)

Firefox has implement a Map/Set.prototype.size method to query the number of mapping/elements. It's not in the strawman. It appears in the latest draft, though weirdly enough Map.prototype.size is a function with an unused argument. What about making it an getter instead of a function? I guess we also don't need the extra parens to request for Map#keys, Map#values, Map#items.

Unlike Java, JavaScript is lucky enough to have getter/setters, we should use them, even in the spec. The boat for myArray.length has sunk a while ago, but new constructs can get this right.

# Erik Arvidsson (12 years ago)

+1

# Alex Russell (12 years ago)

+1

# Rick Waldron (12 years ago)

Would .size be {[[Writable]]:false}?

let s = new Set([1,2,3,4]);

s.size; // 4

s.size = 1; // fail silently (loudly in strict mode)?

I swear there was some discussion either at an in-person on this list, but I'm having no luck tracking it down...

# Erik Arvidsson (12 years ago)

On Fri, Oct 12, 2012 at 11:41 AM, Rick Waldron <waldron.rick at gmail.com> wrote:

Would .size be {[[Writable]]:false}?

It would be a getter on the prototype (and no setter)

# Rick Waldron (12 years ago)

On Fri, Oct 12, 2012 at 11:43 AM, Erik Arvidsson <erik.arvidsson at gmail.com>wrote:

On Fri, Oct 12, 2012 at 11:41 AM, Rick Waldron <waldron.rick at gmail.com> wrote:

Would .size be {[[Writable]]:false}?

It would be a getter on the prototype (and no setter)

+1, That works for me.

# Brendan Eich (12 years ago)

Rick Waldron wrote:

On Fri, Oct 12, 2012 at 11:43 AM, Erik Arvidsson <erik.arvidsson at gmail.com <mailto:erik.arvidsson at gmail.com>> wrote:

On Fri, Oct 12, 2012 at 11:41 AM, Rick Waldron
<waldron.rick at gmail.com <mailto:waldron.rick at gmail.com>> wrote:
> Would .size be {[[Writable]]:false}?

It would be a getter on the prototype (and no setter)

+1, That works for me.

+42.

# David Herman (12 years ago)

+1

# Erik Arvidsson (12 years ago)

On Fri, Oct 12, 2012 at 11:16 AM, David Bruant <bruant.d at gmail.com> wrote:

Firefox has implement a Map/Set.prototype.size method to query the number of mapping/elements. It's not in the strawman. It appears in the latest draft, though weirdly enough Map.prototype.size is a function with an unused argument. What about making it an getter instead of a function? I guess we also don't need the extra parens to request for Map#keys, Map#values, Map#items.

For keys, values and items I think they are cleaner as methods since they return a new iterator every time.

If they were properties, I would expect the following to always be true:

map.items === map.items

# Tab Atkins Jr. (12 years ago)

On Fri, Oct 12, 2012 at 12:14 PM, Erik Arvidsson <erik.arvidsson at gmail.com> wrote:

On Fri, Oct 12, 2012 at 11:16 AM, David Bruant <bruant.d at gmail.com> wrote:

Firefox has implement a Map/Set.prototype.size method to query the number of mapping/elements. It's not in the strawman. It appears in the latest draft, though weirdly enough Map.prototype.size is a function with an unused argument. What about making it an getter instead of a function? I guess we also don't need the extra parens to request for Map#keys, Map#values, Map#items.

For keys, values and items I think they are cleaner as methods since they return a new iterator every time.

If they were properties, I would expect the following to always be true:

map.items === map.items

Agreed, I expect things that look like properties to be stable. Factories should be methods.

# Herby Vojčík (12 years ago)

Erik Arvidsson wrote:

On Fri, Oct 12, 2012 at 11:16 AM, David Bruant<bruant.d at gmail.com> wrote:

Firefox has implement a Map/Set.prototype.size method to query the number of mapping/elements. It's not in the strawman. It appears in the latest draft, though weirdly enough Map.prototype.size is a function with an unused argument. What about making it an getter instead of a function? I guess we also don't need the extra parens to request for Map#keys, Map#values, Map#items.

For keys, values and items I think they are cleaner as methods since

+1

# David Herman (12 years ago)

On Oct 12, 2012, at 12:14 PM, Erik Arvidsson <erik.arvidsson at gmail.com> wrote:

On Fri, Oct 12, 2012 at 11:16 AM, David Bruant <bruant.d at gmail.com> wrote:

Firefox has implement a Map/Set.prototype.size method to query the number of mapping/elements. It's not in the strawman. It appears in the latest draft, though weirdly enough Map.prototype.size is a function with an unused argument. What about making it an getter instead of a function? I guess we also don't need the extra parens to request for Map#keys, Map#values, Map#items.

For keys, values and items I think they are cleaner as methods since they return a new iterator every time.

Agreed.