proposal-class-public-fields icon indicating copy to clipboard operation
proposal-class-public-fields copied to clipboard

Throw on duplicate properties?

Open littledan opened this issue 8 years ago • 5 comments

For private state, in https://github.com/zenparsing/es-private-fields/issues/22 , there is an issue of a field getting redefined with the same name, and what the semantics are. The resolution there seems to be to throw for duplicate fields. Would this make sense to do for property declarations as well? That would obviate the need for the "this.x = this.x" semantics, as a runtime error would be thrown in the case where it makes sense.

littledan avatar Mar 09 '16 20:03 littledan

I prefer throw. :+1:

michaelficarra avatar Mar 09 '16 23:03 michaelficarra

Sounds good to me.

erights avatar Mar 11 '16 15:03 erights

Although I am a reasonable human and prefer to know as soon as possible when I have a mistake, I don't think throwing on duplicate property would be very consistent with either methods (which permit duplicates) or object properties (same).

All of the conversations we've had in the past where we tried to prevent duplicate members from existing on a construct have failed for various reasons, and so I think it would be reasonable to expect the same conclusion and user expectations here.

We could make an argument that fields are especially susceptible to error due to inheritance, but I don't think that holds up very well considering that we wouldn't be banning multiple assignment (which could occur in unfortunate places along the inheritance hierarchy as well).

jeffmo avatar Aug 15 '16 19:08 jeffmo

I agree with @jeffmo's thinking, but it's worth noting that not throwing implies that properties are configurable.

bakkot avatar Aug 18 '16 15:08 bakkot

@bakkot I think we're just discussing whether there should be a special check for the property's existence when setting/defining the property. Let's leave the configurable discussion for a separate thread. I am also sympathetic to @jeffmo 's reasoning.

littledan avatar Aug 22 '16 13:08 littledan