Suggestion: Object.symbols

# doodad-js Admin (8 years ago)

There is "Object.keys" for enumerable-only own property names of an object, and "Object.getOwnPropertyNames" for enumerable and non-enumerable own property names of an object. Now we have "Object.getOwnPropertySymbols" which returns enumerable and non-enumerable own symbols of an object, but we are missing a function "Object.symbols" that returns enumerable-only own symbols of an object.

Thanks,

Claude Petit

# Alexander Jones (8 years ago)

Question is why would you actually want this when you have Map?

I consider all of the 'get some arbitrary subset of properties from an object' use cases obsoleted by Map. But maybe I'm missing something obvious or not-so-obvious...?

# doodad-js Admin (8 years ago)

I consider all of the 'get some arbitrary subset of properties from an object' use cases obsoleted by Map. But maybe I'm missing something obvious or not-so-obvious...?

Objects, obsolete with Map ? “Map” is not meant to replace objects, is it ?

# Tab Atkins Jr. (8 years ago)

On Wed, Jun 15, 2016 at 3:54 PM, doodad-js Admin <doodadjs at gmail.com> wrote:

I consider all of the 'get some arbitrary subset of properties from an object' use cases obsoleted by Map. But maybe I'm missing something obvious or not-so-obvious...?

Objects, obsolete with Map ? “Map” is not meant to replace objects, is it ?

It's meant to replace "objects used as maps". ^_^ That's one of the major reasons to use Object.keys(), so to justify adding a similar method for symbol-valued properties, you'd have to present a use-case that's not better solved by "just use a Map".

# Alexander Jones (8 years ago)

Not in general, just for map-like usages, i.e. if you have an object where the keys are not static, you probably would be better served by a map.

The problem with those functions you describe is that there are a large combinatorial set of them - did you want configurable, own, non-enumerable string-keyed properties? Map is both narrower in this sense (a map item is JUST a key and value with no metadata), and wider in its valid key types (no toString called!), which is more appropriate for many problems.

# doodad-js Admin (8 years ago)

you'd have to present a use-case that's not better solved by "just use a Map"

Simple... An object used like... an object ! An object with methods and attributes. www.npmjs.com/package/doodad

# Alexander Jones (8 years ago)

In my opinion, the consensus appears to be moving towards treating Objects like records, structurally typed bags of properties that don't change shape over their lifetime, and are thus statically analysable. See TypeScript's approach for a justification.

# doodad-js Admin (8 years ago)

the consensus appears to be moving towards treating Objects like records

But we have Map for this ? It’s difficult to follow the consensus... Why they “implemented” class if they don’t want classes, but “records” and no methods, but global/module functions ? Are we moving back to good old BASIC and COBOL languages ?

Anyway, I have my polyfill for “Object.symbols”, I was just hoping for something more performant.

# Alexander Jones (8 years ago)

Well, a map is more often of dynamic size, mapping from one specific type T to another specific type U. (Of course, either can be variants.) An object is a bag of properties of fixed size, with keys of type string|symbol, values of a type specific to the property, metadata from the property descriptor, and a prototype chain.

Nobody mention Array. ;)

# doodad-js Admin (8 years ago)

Well, a map is more often of dynamic size, mapping from one specific type T to another specific type U. (Of course, either can be variants.) An object is a bag of properties of fixed size, with keys of type string|symbol, values of a type specific to the property, metadata from the property descriptor, and a prototype chain.

That’s exactly what I think of objects and Map. So back to “Object.symbols” :)

# G. Kay Lee (8 years ago)

I believe Symbols are all actually non-enumerable which will void the suggestion. And please, stop trying to add all kinds of trivial variations of property extraction methods already. I also oppose the recently proposed Object.enumerableKeys and gang.

# doodad-js Admin (8 years ago)

(quotes from G.Kay Lee)

I believe Symbols are all actually non-enumerable which will void the suggestion.

I can confirm they can be enumerable, or “Object.propertyIsEnumerable” is lying to me.

And please, stop trying to add all kinds of trivial variations of property extraction methods already.

Who, me ? That’s not trivial.... We have “Object.keys” for enumerables, why not “Object.symbols” for enumerables ?

# Jordan Harband (8 years ago)

Symbols are enumerable by default just like normal properties. Object.assign skips non-enumerable Symbols. Object.defineProperty can be used to create a non-enumerable Symbol, but I believe that only impacts Object.assign (and specific enumerability methods, obv).

# doodad-js Admin (8 years ago)

Symbols are enumerable by default just like normal properties. Object.assign skips non-enumerable Symbols. Object.defineProperty can be used to create a non-enumerable Symbol, but I believe that only impacts Object.assign (and specific enumerability methods, obv).

That’s exact.

That’s not something critical, but “Object.symbols” looks like missing from the language.