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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Classes are Expressions

41 点作者 brbcoding将近 10 年前

5 条评论

keithwhor将近 10 年前
Just looks like more JavaScript antipatterns, this time with some ES6 flavour.<p>We know how to &quot;solve&quot; the problem of encapsulation with JavaScript. It&#x27;s not with closures, Symbols, or some other arbitrary hack. We do it by enforcing specific idioms that only require a developer to recognize <i>intent</i> instead of learning 80 different ways to do the same thing. You simply <i>put a single or double underscore</i> in front of the property name.<p>I read what I thought was a clever anecdote by another developer the other day, that the underscore in &quot;obj._varname&quot; indicates &quot;here be dragons.&quot; It&#x27;s an extremely simple idiom to teach new JavaScript developers.<p>I get it, JavaScript is robust. So you can do fun things like this! These are great experiments. But really, what are you accomplishing? You&#x27;ve now just thrown in a closure and a bunch of lines of code (defining your symbols, etc.) that any developer new to your codebase is going to look at like &quot;... what the... what is going on here?&quot; You can solve the problem easily with <i>one character</i>.<p>Consistency. Convention. Creating useful, easy-to-understand, repeatable idioms. That&#x27;s what we should be focusing on.
评论 #9682028 未加载
评论 #9662071 未加载
评论 #9665630 未加载
评论 #9662015 未加载
评论 #9662179 未加载
greggman将近 10 年前
I guess I need to be schooled by why not just use closures?<p><pre><code> var Person = function(first last) { this.fullName = function() { return first + &quot; &quot; + last; }; this.rename = function(newFirst, newLast) { first = newFirst; last = newLast; }; }; </code></pre> Problem solved, you can&#x27;t access `first` and `last` outside the class. You might retort &quot;they&#x27;re bigger&quot; or something but just like everything else in JS they just need the right people to concentrate on them and they&#x27;ll likely get optimized.<p>I don&#x27;t get the relucatance to use the language&#x27;s features rather than hacks (using naming standards and compilers to obfusticate names and then cross fingers) or other hacks (Symbol). Really? You Really want me to write code using Symbol? That&#x27;s going to be awesome in the debugger. Let&#x27;s add a watch to &quot;weke30o304_first&quot;. Refresh, of shit it&#x27;s now &quot;gtgrf40r3fe_first&quot;, my watches all broke (T_T)<p>It also makes me sad the `class` spec didn&#x27;t take that into account so you could use a closure with a `class`.
评论 #9661499 未加载
评论 #9662205 未加载
评论 #9661796 未加载
评论 #9661770 未加载
评论 #9661785 未加载
spankalee将近 10 年前
I know this might be an unpopular view here, but declarative(ish) constructs like class give a little hope that programs will be more statically analyzable so that we can get nice things like static type checking and warnings, code completion, optimizing compilers, etc.<p>These types of imperative patterns make life extremely hard on tools, which now need to add in unreliable things like escape analysis to be able determine which classes are visible. Modules will help a little with explicit exports, so that the importing modules have some hope of tooling, but analysis within a module would suffer.
ravicious将近 10 年前
What about private functions? For now I just add `_` in front of their names, but the code would look so much cleaner if I had some kind of a `private` keyword.
评论 #9661007 未加载
emehrkay将近 10 年前
This is cool, could this behavior be mimicked by doing:<p><pre><code> var name = &#x27;name_&#x27; + Math.random() ; &#x2F;&#x2F;assumed uniqueness</code></pre>
评论 #9661237 未加载