classes and enumerability

Rick Waldron waldron.rick at gmail.com
Tue Dec 23 17:17:43 PST 2014


On Tue Dec 23 2014 at 7:35:44 PM Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> Dear Santa,
>   I wish since ES3 era that properties defined on a prototype are by
> default **not** enumerable.
>

> By any chance we can make this happen in ES6 classes?
>

ES6 is done, properties of user created prototypes are enumerable by
default. They can be made non-enumerable if you want them that way, but
doing so by default is inconsistent with constructor function + prototype
definition.


>
> Or better ... why would anyone expect or want them to be enumerable?
>

- Consistency with constructor function + prototype definition (severe
refactoring hazard).
- Pretty useful for dynamically creating collection APIs, eg:

  Object.keys(Servo.prototype).forEach(function(method) {
    // Create `Servos` collection class wrappers for each method
    Servos.prototype[method] = function() {
      var args = [].slice.call(arguments);
      this.each(function(servo) {
        Servo.prototype[method].apply(servo, args);
      });
      return this;
    };
  });



>
> To define a `class` is a very explicit intent, I believe having those
> definitions non enumerable would be a preferred option for majority of
> developers that have been doomed in `for/in` since kinda ever before
> enumerable was not configurable.
>
> All default methods and properties in native prototypes are **not**
> enumerable, why does it have to be so inconsistent with userland?
>

The widely understood, wholly well-known semantics:

- Properties of built-in prototype objects are non-enumerable
- Properties of user defined prototype objects are enumerable by default,
but can be made non-enumerable if desired.



>
> You also know ES6 is the only window we have 'cause after that it will be
> a breaking change.
>

No: any future annotation syntax must allow for defining writable,
configurable and enumerable attributes.



>
> Thank you at least for any sort of extra/concrete clarification about this
> choice.
>

-
https://github.com/rwaldron/tc39-notes/blob/master/es6/2012-09/sept-18.md#concise-method-definition-revisited
-
https://github.com/rwaldron/tc39-notes/blob/c61f48cea5f2339a1ec65ca89827c8cff170779b/es6/2014-01/jan-28.md#concise-methods-and-enumerability
- https://esdiscuss.org/topic/enumerability#content-23


The consistency argument has won every time this conversation is brought
up. There are ways to get what you want in the future (probably even in
ES7), but changing the default is not the solution.

Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141224/dcb57227/attachment-0001.html>


More information about the es-discuss mailing list