2 ms·
Wonder what the language would now look like if we had some dedicated syntax for Object.create instead of hiding the underlying prototype mechanism under the fa
by codecurve 10y ago
Wonder what the language would now look like if we had some dedicated syntax for Object.create instead of hiding the underlying prototype mechanism under the familiar class syntax.
typeof (class {}) === "function"
This is almost guaranteed to be a future WAT[1] moment for the next generation of JavaScript programmers.
I can already see that a big part of the appeal for classes is that they keep your code looking neat and succinct (as opposed to throwing .prototype and Object.create all over the place.
I'd much rather be writing code that looked something like the below, than having to think about super, static, own properties, constructors, private fields etc.
// provided by some view library
let Component = {
state: null,
setState(state) {
Object.assign(this.state, state);
this.forceUpdate();
}
// ...
}
let Counter is Component {
get state() {
return { count: 0 };
},
inc() {
let { count } = this.state;
let { step } = this.props;
this.setState({ count: count + step });
},
render() {
let { count } = this.state;
return <button onClick={this.inc}>{count}</button>;
}
}
render(<Counter step={2} />);
Although it introduces a new keyword, the abstraction ends up being minimal, because it's just a sugar for the existing Object.create.
// let Counter is Component
let Counter = Object.create(Component);
This is obviously a very half baked idea, but I have programmed quite happily with Object.create for a long time without ever _needing_ to use classes. If we had better tools for working with prototypes then maybe we wouldn't have to pretend the language had classes instead.
[1]: https://www.destroyallsoftware.com/talks/wat https://www.destroyallsoftware.com/talks/wat