google / google/closure-compiler
Static methods unnecessarily copied to subclass constructor
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
This code:
```js
class Super {
/**
* @nocollapse
*/
static method() {
return 'foo';
}
}
class Sub extends Super {}
console.log(Sub.method()); // should log 'foo'
console.log(Sub.hasOwnProperty('method')); // should log 'false' because Sub.method is inherited
```
Is lowered to ES5 that is essentially:
```javascript
function Super() {}
Super.method = function() { return 'foo' }
function Sub() {}
Sub.prototype = Object.create(Super.prototype);
Object.setPrototypeOf(Sub, Super);
Sub.method = Super.method;
console.log(Sub.method()); // logs 'foo', correctly
console.log(Sub.hasOwnProperty('method')); // logs 'true', incorrectly
```
The `Sub.method = Super.method;` line is unnecessary, because `Object.setPrototypeOf(Sub, Super);` has already ensured that `Sub.method` would work correctly, and it also causes `Sub.method` to be an ownproperty of `Sub`, which diverges from the original code's semantics.
Contributor guide
Assessment
This issue has not been assessed yet.