3 ms·
`let` is good but please, give me `let ... in` too: let callback = function(a, b) { // ... } in object.registerCallback(callback);
by smosher 15y ago
`let` is good but please, give me `let ... in` too:
let callback = function(a, b) {
// ...
} in
object.registerCallback(callback);
- masklinn 15y agoI don't know what they've selected for ECMAScript 6, but the `let` keyword as introduced in Firefox 2 has 3 different forms: * Let definitions are equivalent to `var` but block-scoped, so if (x > y) { let gamma = 12.7 + y; i = gamma * x; } `gamma` is local to the `if` block and will not leak out. This form can be used to create bindings in `for` as well: for ( let i=0 ; i < 10 ; i++ ) { console.log(i); } here, `i` is only visible from the condition, incrementor and body of the `for` statement but not outside * Let statement, mostly for side-effects, sufficient for what you need: let (callback = function (a, b) { // ... }) { object.registerCallback(); } * Let expressions, this works like the `let` statement but returns a value, just drop off the braces: let foo = let (i=42) doSomethingWith(i) will bind the result of `doSomethingWith(42)` to `foo`.
- smosher 15y agoLet statement, mostly for side-effects, sufficient for what you need: Sadly it's not. JS has some semantic foibles, but I find myself up against syntax more often than I'd like. Every time I write it I'm impressed at how ugly the syntax is vs for such a good language (modulo the standard library.) The very thing I'm trying to avoid is the use of blocks within parentheses. (Aside: I changed my font to Monaco for JS because of the frequent appearance of `})`, and went to the trouble of fixing it up in Fontforge so gvim et al. would be willing to use it.) I never want to see this: anything(block { // ... }) <--- this ^^____this but I still want the definition to be short-lived (more for self-documenting purposes than anything else, now that I think of it.) Your last example almost has it, but the parens are right there where I want them gone.
- masklinn 15y ago> The very thing I'm trying to avoid is the use of blocks within parentheses. That, er, makes no sense at all. Javascript's function uses braces, having an anonymous function within anything will yield either a brace-and-parens or a brace-and-comma. > Your last example almost has it, but the parens are right there where I want them gone. Then there's no point in using javascript, go do something else because trying to get rid of this will just yield to an idiosyncratic and mostly unusable coding style.
- smosher 15y agoThen there's no point in using javascript, go do something else No need to be rude. Look, I know JS likes to do things in ugly ways. It has great strength in the consistency of its syntax. That's cool. But by the time you say the word "just" you're being ignorant.
- masklinn 15y ago> No need to be rude. The statement was not intended to be rude and I'm sorry you took it this way, it was merely intended to be factual: you seem unhappy with javascript's core syntactic elements, you should use something else because they're not going to be changed. > Look, I know JS likes to do things in ugly ways. Uh... ok, whatever. > But by the time you say the word "just" you're being ignorant. Ignorant of what, of you trying to work around javascript's syntax in javascript for debatable reasons of personal aesthetics? No, I think I got that now, I was originally mistaken indeed in that I thought you were looking for an improvement to the language, not "fixing" an irrelevant syntactic pet peeve.
- smosher 15y agoThe statement was not intended to be rude It comes off as a euphemism for much shorter phrase. Perhaps you didn't intend to be rude, but you failed to be polite. Ignorant of what (Exactly.) You've made assumptions and pinned them to me. I'd set you straight but I don't actually want to continue this conversation. Sorry it didn't work out.