Comments (6)
I agree there's a typo in the spec text; both O
and F
there are unbound and should instead be receiver
. You're correct that the intent is for fields to be defined on instances.
If the fields are only defined on the instance, then the parent class has no way of knowing about them in the constructor.
Fields are installed before the body of the constructor executes (or at the start of the super
call in the case of derived classes): in other words, they're already present on this
by the time it's possible to refer to this
. So your example would work fine, I think, and end up with garfield.name === 'Garfield'
.
In any case, this has been discussed at length already in tc39/proposal-class-public-fields#59. Ultimately, putting fields on the prototype seems likely to do more harm than good, even if there are use cases for it.
from proposal-class-fields.
Thanks for the explanation. Ah, so in this proposal, the properties are initialized before the call to super()
? I thought the declaration of Cat
above was equivalent to this:
class Cat extends Entity {
constructor(attributes) {
super(attributes)
this.name = null
}
}
This is also the only way to transpile it to ES6 with conventional class syntax, since you can't do this.name = null
before calling super()
...it's how Babel currently does it. And in this version garfield.name === null
.
But if the real implementation will do this behind the scenes somehow:
class Cat extends Entity {
constructor(attributes) {
this.name = null
super(attributes)
}
}
Then yes, that would solve it.
Alternatively (or in addition), maybe there could be some methods on Reflect
that would have access to instance property definitions prior to the call to super()
.
from proposal-class-fields.
Sorry, I'm mistaken, instance fields on subclasses are initialized after the call to super
exits, not when it enters. So you're right, that wouldn't work.
from proposal-class-fields.
It kind of seems like your assignAttributes
implementation is brittle; specifically for this reason. Typically I'd expect these attributes to all be stored in an object or Map that's under a single property on this
.
from proposal-class-fields.
I think it's reasonable to want to implement this idiom as written...basically the idea is to create a plain old JS object but with the ability to set all the parameters at once in the constructor - and ignoring any extraneous properties that might be passed in. I'm not sure how using an object or Map would help with this particular problem, unless you mean passing the attribute map as an argument to the constructor like this:
class Entity {
constructor(attributes) {
this.attributes = attributes;
}
}
class Cat extends Entity {
static initialState = {
name: null
}
constructor(attributes) {
super({...Cat.initialState, ...attributes})
}
}
...but that defeats the purpose of using a base class to assign the attributes (which was intended to reduce boilerplate code). And it allows invalid attributes to be added to the object. So I guess I'm just not following you.
Anyway, this is just one use case for wanting to reflect on instance property names. I can imagine other potential scenarios...for example, if you had a constructor with required parameters, with the current proposal the only way you could find out the property names from outside the class would be to actually create an instance (requiring specific knowledge of how to call that particular constructor).
But there are many ways to accomplish reflection, and I take your point that defaulting to putting properties on the prototype could be problematic. So perhaps I should close this issue and file a new one specifically about reflection.
from proposal-class-fields.
Closed in favor of #36.
from proposal-class-fields.
Related Issues (20)
- Class fields are a time waster. HOT 7
- Motivation for PrivateFieldAdd type error? HOT 5
- Interaction with Integrity Levels? HOT 4
- private field and protected field HOT 1
- Clarify what the context (`this`) for fields declared as anonymous functions will be in the spec. HOT 2
- Inferred function names for number/bigint fields aren't handled right HOT 2
- [Suggestion] Consider to use time proof solution with private and protected keywords HOT 39
- Private fields breaks POI HOT 35
- Support for privates in object literals? HOT 3
- Class composition HOT 3
- [[Define]] semantics causes libraries to lose performance. HOT 2
- class fields with [[Define]] semantics are much slower than with [[Set]] semantics. HOT 3
- [[Define]] semantics necessitate workarounds HOT 7
- Properties without initializers can create some headache in "real-world" scenarios. HOT 14
- Why are #private fields not source-positional? HOT 22
- Statement of our opinions on class fields and its related package of proposals HOT 2
- Proxy nested inside of the Prototype Chain HOT 2
- Please Don't Do That. HOT 8
- Why are class fields so slow? HOT 2
- Constructor arguments with object destructuring HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-class-fields.