Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silent failure of an assignment seems likely to cause hard-to-find bugs. #3

Open
brad4d opened this issue Jul 6, 2023 · 2 comments

Comments

@brad4d
Copy link

brad4d commented Jul 6, 2023

One of the most important changes made by the introduction of strict mode was to throw errors in several cases where JS assignments used to silently fail.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

This proposal seems to be re-introducing silent assignment failures.

This is especially true if the intention is to allow any form of optional chain on the LHS so long as it ends with ?.prop or ?.[expr]. In that case even when you determine that a silent failure has occurred, you don't know which value was undefined.

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Jul 7, 2023

All the cases that were made to throw with strict mode would still throw with ?. (in strict mode):

  • a?.b = c throws if a is not declared
  • a?.b = c throws if b is a read-only property
    Which is similar to how delete a?.b already throws in strict mode if b is a non-configurable property.

Strict mode was about avoid suppressing implicit failures, while in this case the potential failures are explicitly marked. The language already has conditional assignment operators (??=, ||= and &&=) that silently avoid assigning values. Their behavior is unsurprising because they are explicitly marked, similarly to how the optional behavior of this proposal is explicitly marked via ?.

In that case even when you determine that a silent failure has occurred, you don't know which value was undefined.

This is a property of optional chaining in general, not specific to using it in assignment positions. In console.log(a.b?.c?.d) even if you determine that the result is undefined you don't know which one of the properties was undefined.

@lopugit
Copy link

lopugit commented Mar 14, 2024

get better at debugging :P But yes 😂 potentially a non blocking error message or something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants