This is a proposal for the addition of the composes
keyword into the ECMAScript programming language spec.
Introduction
The composes
keyword adds support for composition (as in composition over inheritance) in ECMAScript classes.
class LoginForm composes Validation, AsyncIO { ... }
The proposed syntax is compatible with existing keywords as well as concepts in superset languages such as Typescript.
class LoginForm extends Layout composes Validation, AsyncIO implements ContractInterface { ... }
Prior work
Modern web frameworks often use similar concepts in one form or another:
- React supported mix-ins historically and now recommends composition via higher order components
- Vue.js currently supports mix-ins and has an RFC to add first-class support for composition
Composition is often championed by miscellaneous authors: 1, 2, 3
Rationale
As shown above, many libraries and authors are actively working to support composition in a number of alternative ways.
Currently, composition is often achieved via nested higher order components:
withAsyncIO(withValidation(class LoginForm { ... }))
The proposed syntax improves clarity and readability considerably:
class LoginForm composes Validation, AsyncIO { ... }
Adding composition into the language spec as a first class citizen would offer clear benefits in maintainability and readability, as well as standardizing a currently fragmented problem space.
Limitations and issues
Composition brings numerous technical and design challenges, pertaining but not limited to scoping, this
, etc.
At the current proposal stage, the author does not try to address them. In the future, already existing implementations can offer a good base for discussion.
Conclusion
Composition as a concept is already actively used by many libraries and authors, and championed at a considerable level.
Adding first class support for composition into the language itself would offer numerous clear wins for readability, maintainability, and more.