archives

« Bugzilla Issues Index

#4140 — class definitions: computed property name can be "constructor"


Should this be a runtime error during class definition evaluation?

currently it just over-writes the constructor property.


One computed name that should definitely be forbidden is `static ['prototype'](){}`.


['constructor'] should be allowed.

As is, we should not special case this one. The prototype property 'constructor' is not read only so disallowing it would require that every computed property name would need to be compared with the string 'constructor'.

If we made the prototype 'constructor' property read only (like the constructor property 'prototype') then we would get the type error for free.


(In reply to Leon Arnott from comment #1)
> One computed name that should definitely be forbidden is `static
> ['prototype'](){}`.

That is already the case since the property is non-configurable and non-writable.


(In reply to Erik Arvidsson from comment #2)
> ['constructor'] should be allowed.

I think either alternative is plausale.

>
> As is, we should not special case this one. The prototype property
> 'constructor' is not read only so disallowing it would require that every
> computed property name would need to be compared with the string
> 'constructor'.

but only as part of ClassDefinitionEvaluation

>
> If we made the prototype 'constructor' property read only (like the
> constructor property 'prototype') then we would get the type error for free.

would have to also be non-configurable because [[DefineOwnProperty]] is used to install methods during ClassDefinitionEvaluation


(In reply to Allen Wirfs-Brock from comment #4)
> would have to also be non-configurable because [[DefineOwnProperty]] is used
> to install methods during ClassDefinitionEvaluation

Yup. read only is not a spec term, it is my way of saying non-configurable, non-writable.

I think it is too late to make the constructor read only and therefore I don't think we should add this restriction to computed property names in ClassDefinitionEvaluation.


ok, won't change anything

I mainly opened this to see what people thought about it.


(In reply to Erik Arvidsson from comment #3)
> (In reply to Leon Arnott from comment #1)
> > One computed name that should definitely be forbidden is `static
> > ['prototype'](){}`.
>
> That is already the case since the property is non-configurable and
> non-writable.

Ah, OK. I'd gotten confused because 14.5.1 has an early error for `static prototype`, as a bare name, which, if what you say is true, is technically unnecessary except to catch the bug earlier.

Speaking of which: it's also an error if more than one `constructor` bare name is present, which was one of my motivations for mentioning this topic. Given that similar limits on duplicate properties were removed for object literals due to computed properties daunting them, maybe that 14.5.1 restriction should be relaxed too? Hmm.


(In reply to Leon Arnott from comment #7)
>
> Speaking of which: it's also an error if more than one `constructor` bare
> name is present, which was one of my motivations for mentioning this topic.
> Given that similar limits on duplicate properties were removed for object
> literals due to computed properties daunting them, maybe that 14.5.1
> restriction should be relaxed too? Hmm.

this was considered but rejected when the restriction of duplicate properties was relax.

The constructor property is really a special form that is part of a ClassBody. It does define just another property. Rather, it is how the actual body of the constructor is provided. Clearly only one such such body is possible.

["constructor"]() {}
on the otherhand is is evaluated after the constructor is already created so it has no effect upon the constructor body. It just over-writes the prototype.constructor property value.