Check for duplicate class element names

22 views
Skip to first unread message

Raul-Sebastian Mihăilă

unread,
Jun 6, 2016, 3:31:37 AM6/6/16
to Exploring ES6
In section 15.5.1 from http://exploringjs.com/es6/ch_classes.html it is mentioned that duplicate class element names are not allowed. However both Chrome and Firefox allow:

```js
class C {
  func() {}

  func() {}
}
```

Also I couldn't find this check in the spec.

Mörre Noseshine

unread,
Jun 29, 2016, 1:55:35 PM6/29/16
to Exploring ES6
When you do that you merely overwrite what you did previously. Remember that "class" is just another word for a special kind of use of Javascript's prototype-based inheritance, so that any member functions end up as members on the constructor function's prototype object. You know what happens when you assign to the same object property more than once...

Example:

$ node
> class C { func (a) {} func (b) {} }
[Function: C]
> C.prototype.
C.prototype.__defineGetter__      C.prototype.__defineSetter__      C.prototype.__lookupGetter__      C.prototype.__lookupSetter__
C.prototype.__proto__             C.prototype.constructor           C.prototype.hasOwnProperty        C.prototype.isPrototypeOf
C.prototype.propertyIsEnumerable  C.prototype.toLocaleString        C.prototype.toString              C.prototype.valueOf

C.prototype.func

> C.prototype.func
[Function: func]
> C.prototype.func.toString()
'func(b) {}'
Reply all
Reply to author
Forward
0 new messages