TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How necessary are var, let, and const?

89 点作者 tbassetto将近 10 年前

15 条评论

moogly将近 10 年前
I found this article's premises and findings rather pointless, and kept on wanting the author to instead verify the premise "are there actually valid uses of 'var' now that 'let' exists?" (I haven't found any yet, and I'd like to know if I'm wrong). But maybe I'm too much of a pragmatist rather than an academic, so I suppose it wasn't written for me.
评论 #9639956 未加载
评论 #9640850 未加载
pm24601将近 10 年前
const IS valuable and necessary.<p>1) It means that every place the const appears it can be replaced with the const&#x27;s value. Type checking can be streamlined, less indirection.<p>2) It communicates the developer&#x27;s INTENT in an enforced manner. No later code change can accidentally make the const suddenly non-const.<p>3) const + deep Object.freeze() makes for a complete const.<p>Sure a human could hypothetically scan the code to see that the value has not been reassigned. This falls apart in practice. As soon as the company has a few dozen developers the code is constantly changing and such manual analysis would have to be constantly done.
评论 #9639631 未加载
评论 #9639674 未加载
评论 #9641759 未加载
评论 #9640332 未加载
magice将近 10 年前
Hmm, posts like this make me want to make Lisp mandatory for all programmers. Given that LISPers have argued for &quot;lambda the ultimate kitchen utensil&#x2F;sinks&#x2F;cooks&quot; for the last few decades, it&#x27;s slightly annoying that people from other languages repeat the point now. That said, congratulation for learning value of syntactical sugar. I hope you like it.<p>That said, can somebody implement hygienic macros in JavaScript. They will probably help will people having to explain basics knowledge over and over.
评论 #9641753 未加载
评论 #9641498 未加载
评论 #9641983 未加载
评论 #9641279 未加载
评论 #9643048 未加载
评论 #9641949 未加载
ylg将近 10 年前
Interesting and well written; if it were me, I might only change the ending a bit:<p>&quot;Whereas, const […]. <i>It is not nearly as useful as immutable data, because the problem it solves is easy, not hard.</i>&quot;<p>To something like:<p>&quot;const may be more useful than adding something like immutable data to an imperative language with mutability deep in its DNA might be because it helps with one of the hardest problems; long-term, <i>correct</i> re-reading by humans doing maintenance or extension.&quot;<p>But, the OP covers that well earlier in, so I&#x27;m not sure a third take on it would add much. Thanks for doing the exploration!
serve_yay将近 10 年前
Can you eliminate `var` by introducing an unnecessary function, declaring the `var`s as parameters, and then invoking? Yes you can.<p>Why you would ever do that, however...
评论 #9641879 未加载
bmeck将近 10 年前
const can be used to denote that a reference to the variable is being passed around and should not change for the lifetime of a scope.<p><pre><code> function f() { const queue = []; consumer(queue); return queue; } </code></pre> It could be important to note that removing const has benign effect, but that it is there to ensure a programmer does not change the value of queue between `consumer(queue)` and `return queue`. Easily reasoned about, but extra safeguards from introducing bugs is always nice.
评论 #9639168 未加载
评论 #9639013 未加载
评论 #9638602 未加载
rbalicki将近 10 年前
If we use the approach of converting var&#x27;s to named parameters, we also have to give up on passing variable numbers of arguments to functions. That is, unless we do something silly like:<p><pre><code> function silly(x, y, z, args) { args = Array.prototype.slice.call(arguments); args.shift(); args.shift(); args.shift(); args.shift(); x = y = z = undefined; &#x2F;&#x2F; regular function goes here }</code></pre>
评论 #9639231 未加载
pc2g4d将近 10 年前
Does anyone else out there think Javascript `const` should really be called `final`? It more closely mimics Java&#x27;s `final` keyword by preventing reassignment (&quot;rebinding&quot;) of the variable, whereas `const` makes me think of full immutability as in C++.
bshimmin将近 10 年前
OK, I&#x27;ll bite: what does De Stijl (<a href="http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;De_Stijl" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;De_Stijl</a>) have to do with any of this?
评论 #9639646 未加载
评论 #9639708 未加载
评论 #9639622 未加载
shawndumas将近 10 年前
To be fair; getting the benefit of const <i>would</i> require code too though:<p><pre><code> function constUseful () { var guarded = 1; (function loop (orgGuarded) { setTimeout(function () { if (guarded !== orgGuarded) { throw &#x27;guarded reassigned!&#x27;; } loop(orgGuarded); }, 0); })(guarded); guarded = 2; } constUseful(); </code></pre> That&#x27;s an approximation of const&#x27;s value as delineated in code.
protonfish将近 10 年前
A better question is how useful is <i>let</i> at all? Having two different variable scoping rules with no way to know which one a variable is using without backtracing sounds like a maintenance nightmare. I understand what is it good for, but it seems like we are replacing a small problem with a bigger problem.<p>An even better question is why not get rid of <i>var</i> and make every variable local to its parent function? To alter globals, use the window object explicitly.
评论 #9639924 未加载
评论 #9640042 未加载
评论 #9639972 未加载
rashkov将近 10 年前
Could anyone please explain to me why the author chose those functions for his examples? I got as far as callFirst() and repeat() and found it hard enough to wrap my mind around the functional &#x2F; meta programming within. Are those actually good examples? It seems like there are simpler ways to illustrate the point.
评论 #9643397 未加载
aikah将近 10 年前
let is obviously needed(devs like lexical scoping) but I found the whole var + let in the same language thing a bit confusing. The temptation to introduce yet another strict mode(to forbid var,and a few other constructs) is strong on this one.
评论 #9642551 未加载
评论 #9639966 未加载
评论 #9640189 未加载
riffraff将近 10 年前
&gt; You can see every single rebinding of a variable within the lexical scope of the function,<p>I do not think that is true, a called function can side effect the caller environment via `arguments` if that is passed around.
评论 #9641529 未加载
btbuildem将近 10 年前
That disclaimer is a bit of a cop-out, given the subject matter..