Skip to content

ES5: inheritance of static properties is incorrect #4266

Closed
@jods4

Description

@jods4

Assume

class B {}
class C extends B {}

The default __extends is copying each own property of B to C. This is a problem because it means that a property of B becomes indistinguishable from a property of C (by virtue of copy, it is its own).

This doesn't seem like much, but sometimes you need each class to have a different value for a property.

For a real life use-case, I was bitten here: aurelia/metadata#22.
Other use-cases that occured to me because of this bug: WeakMap polyfills stores data in the object under a random key. Because of the codegen above, WeakMap doesn't work properly for B and C.
By virtue of that, Reflect.metadata polyfill is not working in IE9 and 10 (it relies on WeakMap).

Contrast with Babel, who just chains prototype. This way, one can check hasOwnProperty().
An artificial example showing the different behavior between TS and Babel:

http://www.typescriptlang.org/Playground#src=class%20B%20%7B%7D%0AB%5B%22__metadata__%22%5D%20%3D%204%3B%0Aclass%20C%20extends%20B%20%7B%7D%0Aif%20(C.hasOwnProperty(%22__metadata__%22))%0A%09alert(C%5B%22__metadata__%22%5D)%3B

http://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&code=class%20B%20%7B%7D%0AB.__metadata__%20%3D%204%3B%0Aclass%20C%20extends%20B%20%7B%7D%0Aif%20(C.hasOwnProperty(%22__metadata__%22))%0A%20%20alert(C.__metadata__)%3B

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions