Comments (10)
Cool, thanks for clarifying. Yeah this is an interesting one. Instantiating a new function to essentially contain the static field initializations certainly seems like the least drastic approach, e.g.
class Example {
static prop = "value";
static prop2 = () => this.prop;
}
becoming kind of like
class Example { }
(function(){
this.prop = "value";
this.prop2 = () => this.prop;
}).call(Example);
I'd figure that it should just be around the final new steps that prep the environment and call InitializeStaticFields
?
Edit: The rest of this was me misreading the spec, woops!
@littledan One question related to this, for both static and non-static props. What's the expected behavior for computed keys, e.g.
(function(){
class Example {
toString(){
return "innerKey";
}
static toString(){
return "staticKey";
}
[this] = "non-static";
[this](){}
static [this] = "static";
static [this](){}
}
}).call({
toString(){
return "outerKey";
},
});
It seems like with the current spec for non-statics, and the behavior proposed in this issue, this ends up as
class Example {
innerKey = "non-static";
outerKey(){}
static staticKey = "static";
static outerKey(){}
}
but that behavior for props is pretty inconsistent with the current behavior with methods. Should the execution of property keys be happen at class evaluation time?
from proposal-class-fields.
Computed field keys are still evaluated once, in the same scope and at the same time where the computed method keys are evaluated.
from proposal-class-fields.
For example, this
in a computed method or field key refers to the outer this.
from proposal-class-fields.
Oh shoot, you are totally right, I misread. Disregard the second half of that then.
from proposal-class-fields.
To answer the first part, I thought this would be better to add in EvaluateBody, which would match now normal functions do it.
Please, keep the clarifying questions and bug reports coming; I really appreciate the work you're doing here.
from proposal-class-fields.
Could you clarify what you mean about that? Not quite making the jump from ClassDefinitionEvaluation
to where FunctionDeclarationInstantion
/EvaluateBody
would tie in. My psuedo-example isn't super clear, but I was thinking in ClassDefinitionEvaluation
would make sense since that was where the static fields and the constructor are initialized.
from proposal-class-fields.
I think the outer context is captured by the FunctionCreate call from ClassFieldDefinitionEvaluation .
from proposal-class-fields.
Sorry, looking at all this again, I think the current spec already has the mechanics for the sequence of events, which is:
- During class evaluation, ClassFieldDefinitionEvaluation calls FunctionCreate which captures the current scope.
- During [[Construct]],
super
or later in class evaluation, DefineField invokes Call on each Initializer. PrepareForOrdinaryCall restores the appropriate context.
There are a couple editorial issues in this code path which I'll fix up.
from proposal-class-fields.
Thanks for looking again, this does seem to cover it, so we should be all set. Following the chain of events through the calls was tripping me up a bunch. I think I had just missed that there was that FunctionCreate
call in ClassFieldDefinitionEvaluation
that would handle it, or at least not followed how the pieces fit together fully.
from proposal-class-fields.
This is all tricky stuff; you can see that I didn't understand it immediately either!
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.