Feedback on alternatives? #4
Comments
I love it. Doing Web IDL implementations in JS, I end up having to decorate methods with brand check wrappers even if I use private fields (due to not wanting the TypeError message to reveal implementation details). So far I’ve addressed that by keeping a Weak collection in the mix even when private fields would have been sufficient for maintaining the actual private state. This would make it more natural to drop the extraneous weak collection in favor of using private fields even in the condition for when we “manually” throw the “Illegal invocation” TypeErrors. One less moving part in the mix: Re: syntax — this seems intuitive to me (partly due to the hint of symmetry with typeof foo not throwing a ReferenceError when foo is undefined). |
I can list alternatives I can think of?
|
The |
I have a question and it probably doesn't deserve its own issue, so posting here. Could this be solved with an additional check in optional chaining operator? |
If this change was made, returning const hasField = o !== undefined && o !== null && o?.#x !== undefined; And, as you say, this pattern would be limited to situations where the private field was guaranteed to never store undefined, which may not always be desirable. |
const hasField = o?.#x !== undefined; Would give the same result every time. When I was drawing from the rationale for this proposal, which asks for a way to silently "check if an object has a private field". |
Right you are, mine was unnecessarily verbose. Might also be worth noting that this would be a breaking change for any code that is relying on invalid private field access throwing. It also appears this was discussed as part of the optional chaining proposal here: tc39/proposal-optional-chaining#28 |
Thank you, that completely answers my question. This additional silent check was considered, and rejected in favour of consistency with other kinds of optional chaining. Which makes sense, even though none of the examples presented in that thread convinced me that |
It’s quite useful, and was a necessary part of the design of class fields - but that’s also irrelevant, since that proposal is stage 4 and this one can’t change it, for the reasons mentioned above. |
ljharb commentedApr 1, 2020
Hi everyone! It'd be great to get as much feedback from the community as possible before this proposal advances to stage 2.
#privateField in obj
?(If you have thoughts on shorthand syntax, please add those to #2)
Thanks!
The text was updated successfully, but these errors were encountered: