Replies: 10 comments 6 replies
-
explaining my voteI voted:
(function ok(){ let foo = 1 ; if (true) { let foo = 0; console.log(foo) } })() //=> 0
|
Beta Was this translation helpful? Give feedback.
-
|
I can see it going either D (the conditional declaration is in the same scope as the original declaration) or B (the conditional declaration is in a new scope shared with the conditional body) Based on the behavior of for loops, I'd expect it to be B. |
Beta Was this translation helpful? Give feedback.
-
|
Links: |
Beta Was this translation helpful? Give feedback.
-
|
I may not be the right person to poll on this, since as a TC39 delegate, I've been involved in discussions about this proposal for a long time. But this survey might just prove my concern—the semantics of |
Beta Was this translation helpful? Give feedback.
-
explaining my vote
I voted As others mentioned,
While there's currently no easy way to do 1. without declarations in conditionals, 2. can easily (and I would argue more clearly) be achieved with an explicit scope: let foo = 1;
{
let foo = 0;
if (foo) { console.log(foo) }
else { console.log(foo) }
}So I intuitively expected it to match 1. (i.e., printing the outer |
Beta Was this translation helpful? Give feedback.
-
|
I think the question needs further refinement. From the comments, some people think it would log I voted for |
Beta Was this translation helpful? Give feedback.
-
|
should throw. Also no shadowing. (but the example doesn't make clear that this is the only issue) |
Beta Was this translation helpful? Give feedback.
-
|
voted if (let foo = 0)is shorthand for let foo = 0
if (foo)making the value of a declaration is the same as an assignment's. in the example: 2. the scope of an in the example: |
Beta Was this translation helpful? Give feedback.
-
|
Coming from Go I would expect |
Beta Was this translation helpful? Give feedback.
-
|
I expect the I am curious about the behaviour for The use case I would see for this kind of feature only becomes (ever so slightly) beneficial, for me, in an document.addEventListener('click', function(event) {
if (const target = event.target.closest('a')) {
console.log(target.href)
} else if (const target = event.target.closest('button')) {
console.log(target.type);
} else if (const target = event.target.closest('input, textarea, select')) {
console.log(target.tagName.toLowerCase());
} else {
console.warn('Non-interactive element:', event.target);
}
})Or let's say we start with a basic bit of code expecting a number, but then another developer adds an if (const linkData = document.querySelectorAll('a').length) { /* Returns a number */ }
// Another developer adds this line below
else if (const linkData = document.head.querySelector('meta[property="twitter:image"]')) { /* Returns a DOM element or null */ }
// ^^^^^^^^^^^^^^
else { showLinkCount(linkData.toString()); }
// (0).toString() was the expected thing, but now null.toString() is possible and throwsI know it's up to developers to write correct code (and this example, well, stinks), but this feels like an easy way to get confused when expecting a value in the And then, what happens with different variable names in each This all feels pretty confusing, and kinda magic as it's jumping scopes. If you want that behaviour, the "old" way of PS: falsy values are pretty easy to handle, so in my stinky example |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
If declarations in conditionals were possible (@tc39 proposal being discussed right now!) what would you expect this to log?
69 votes ·
Beta Was this translation helpful? Give feedback.
All reactions