4 ms·
I wonder whether `let` should be used everywhere now making `var` effectively obsolete? Mixing both declaration styles looks bad.
by vbezhenar 12y ago
I wonder whether `let` should be used everywhere now making `var` effectively obsolete? Mixing both declaration styles looks bad.
- svachalek 12y agoAs I understand it, yes, 'var' is now obsolete. 'let' does the same thing with less confusing semantics.
- e12e 12y agoSo, 20+ years ago, javascript was a scheme(-like langauge) -- but was disguised as java for marketing -- now we can go back to a more scheme-like javascript? I wonder if we'd be better off with a proper scheme in the browser in the first place...
- albemuth 12y agoClojureScript is pretty cool!
- Touche 12y agoI disagree, function scope is not going away. So I still have to understand that. If I mix in lexical scope, I now have to think about 2 scopes, which makes my life worse.
- pfooti 12y agoI think one could argue that function scoping is no longer necessary - there's nothing you would do with function scoping that you couldn't do with lexical-block scoping, since with lexical scoping, you can still declare variables at a function block scope. The only real benefit I can think of from function scoping is hoisting, reliance upon which strikes me as an antipattern.
- gtirloni 12y agoI'm not a JS programmer so I was confused with using "let" and the arrow function to create a function versus using "function funcname {}".
- svachalek 12y agoNaming functions with 'var' or 'let' is possible in JavaScript because functions are first-class in JS unlike some common languages that don't have a functional paradigm (say Java). I think the function foo() {} syntax is mostly to make programmers coming from languages like Java feel comfortable but it does get confusing to have multiple syntax/semantics for declaring functions. The fat arrow '=>' in ES6 adds yet another way. Personally I think the added complexity in ES6/7 mostly just add bloat and syntactic sugar rather than necessary fixes.
- e12e 12y agoI wonder if it wouldn't have been better to just declare "javascript 2", and write a compiler from "javascript 1/legacy" to "javascript 2" in "javasript 2" -- and suggest browsers/implementers used that. I mean, do we really need both let and var; function * , function and => (I suppose having to mark a function as re-entrant at least one can understand, though)? In the presentation, it's not clear (granted without commentary) why one would ever, for new code, write: // Interpolate expressions into a template string var name = "Bob", time = "today"; console.log(`Hello ${name}, how are you ${time}?`); function dedent(strings, ...values) { let result = ''; for (let i = 0; i < strings.length; i++) { result += strings[i].replace(/\n\s+/g, '\n') + values[i]; } return result; } console.log(dedent `Hello ${name}, How are you ${time}?`); And not: let name = "Bob", time = "today"; console.log(`Hello ${name}, how are you ${time}?`); let dedent = (strings, ...values) => { // is this valid? let result = ''; for (let i = 0; i < strings.length; i++) { result += strings[i].replace(/\n\s+/g, '\n') + values[i]; } return result; } console.log(dedent `Hello ${name}, How are you ${time}?`); I understand backwards comparability, but for at least Firefox, let won't work without marking the code in question as v1.7 anyway. It might as well have been Self, Ruby or R6RS. While having as compact syntax as Smalltalk or Lisp might be going a bit far in the other direction -- I do wish they'd taken the time to simplify the language.
- peferron 12y agoI use `const` instead of `var` almost everywhere and use `let` in the few cases that actually require reassignment.